I'm reading an online python tutorial book from here. The code is listed below. When I execute the code, I can type words into it but then it gave me the error below. What is the wrong with the code?
On a related note, if you have a better resource for leaning python, please let me know. I'm looking for one that is online and updated often (ex: railstutorial.org). The resource I am using have plenty of errors even this early in the book. Thanks.
Enter something : programmig is fun
Traceback (most recent call last):
File "break.py", line 5, in <module>
s = input('Enter something : ')
File "<string>", line 1, in <module>
NameError: name 'programmig' is not defined
#!/usr/bin/python
# Filename: break.py
while True:
s = input('Enter something : ')
if s == 'quit':
break
print('Length of the string is', len(s))
print('Done')
input()
doesn't get a string, so it thinks thatprogrammig
is a variable. You can type the input you want in quotes to solve this.A better way however, is to use
raw_input
, which returns a string.So either do
Enter something : 'programmig is fun'
, not recommended, or dos = raw_input('Enter something : ')
recommended wayThe cause of the confusion is that the book is probably for python 3, which has a different
input
, and also a differentprint
, while you are using python 2.x.This is python 3 code. It seems like you're running it with python 2.
Run
python --version
to check which version of python you are using.