0

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')

2 Answers 2

2

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.

Sign up to request clarification or add additional context in comments.

3 Comments

I have python 2.7.2 which is sad because the book explicitly said that he is using python 2.7.2.
Well, actually he said he was using python 3 then switch and now he has a mixture of python 2 and 3 codes lol
Odd. I can tell this is 3 because: print is a function and input is expected to act like the old raw_input. Also, it runs just as you'd expect in python 3.
1

input() doesn't get a string, so it thinks that programmig 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 do s = raw_input('Enter something : ') recommended way

The cause of the confusion is that the book is probably for python 3, which has a different input, and also a different print, while you are using python 2.x.

3 Comments

This is true for python 2, but judging by the function print, this is python 3.
@MatthewAdams, so probably he's running python 2.x while having a python 3 tut.
I'd bet that's what's going on.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.