0

I was trying a very simple code on my MacBook: here is the code

def file_name(fname):
    try:
        fhand = open(fname)
    except:
        print('File cannot be opened: ', fname)
        quit()
    count = 0
    starts_with = **input**('Entries starts with: ')
    for line in fhand:
        if line.startswith(starts_with):
            count = count + 1
print('There were ', count, ' subject lines in', fname)

fname = input('Enter the file name: ') file_name(fname)

In order for that code to work, I had to replace input() with raw_input(). I have installed python 3.8 on my device though cannot run the code with it. At first, I thought the problem was with VS code. After switching to PyCharm professional I still have the same, exact problem.

The desired output after renaming to raw_input() is:

Hou-Pengs-MBP:PY4E houpengzhu$ python File.py
Enter the file name: hw.py
Entries starts with: print 
('There were ', 1, ' subject lines in', 'hw.py')

Output when using input()

Hou-Pengs-MBP:PY4E houpengzhu$ python File.py
Enter the file name: hw.py
Traceback (most recent call last):
  File "File.py", line 15, in <module>
    fname = input('Enter the file name: ')
  File "<string>", line 1, in <module>
NameError: name 'hw' is not defined

My configuration for PyCharm:

PyCharm, click to view screenshot

for VS code:

VS code, click to view screenshot

What should I do to yield the desired result?


UPDATE: I dug around and found this solution from Ryosuke Hujisawa (second upvoted answer) worked for me: how to change default python version? Now my default version of python has been changed to python 3. When running that code, you still need to state you want python3 to IDE but doing so you won't need use raw_input() from python 2 anymore for the code to work.

H.P. Zhu
  • 11
  • 3
  • Please fix the indentation of your code – OneCricketeer Jun 20 '20 at 04:54
  • `name 'hw' is not defined` has nothing to do with the input function, by the way, but the fact that it's trying to import the filename you've typed, or interpret it as a variable name – OneCricketeer Jun 20 '20 at 04:55
  • @OneCricketeer not so; the problem is that the code is being run under Python 2.x, wherein `input` implictly `eval()`s the input string. The `NameError` occurs because Python attempts to look up the `py` attribute of the undefined `hw`. The question is off-topic for Stack Overflow since it concerns setting up the environment, rather than actually writing code. – Karl Knechtel Jun 20 '20 at 05:01
  • @Karl Sure, but no where in [help] does it say that environment setup is off topic – OneCricketeer Jun 20 '20 at 05:06
  • @OneCricketeer Sorry about that, the code box indentations blew up after I pasted the code in. I failed to formate them afterwards. I am still learning my ways around the platform. – H.P. Zhu Jun 20 '20 at 05:38

2 Answers2

0

Are you sure that you are running it on python 3.In python 3 there is no raw_input() It should work with input() if you use python3 File.py.

AfiJaabb
  • 316
  • 3
  • 11
  • Yes that's what I was trying to figure out. I do have python 3 installed and running, but the code will run into name error if left unchanged. Using raw_input () solves the issue. I think this is caused by the default python version settings in Terinmal. – H.P. Zhu Jun 20 '20 at 06:06
0

Your IDE doesn't control your terminal session. python -V will show you what the problem is, or more precise, the version you're executing the script with

You should try to run /usr/bin/python3 hw.py, for example for python 3.7.3 or with /usr/local/bin/python3 for 3.8.3

Or you need to activate your venv then you can just use python

OneCricketeer
  • 179,855
  • 19
  • 132
  • 245
  • Looks like my default python version in terminal is python 2.7, I'll see if I can modify it to python 3. Thanks. – H.P. Zhu Jun 20 '20 at 06:06