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.