Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Python

Victoria Holland
Victoria Holland
6,159 Points

lumberjack.py script won't run on my computer

I can run the lumberjack.py script successfully in the Treehouse console, but when I copied and pasted it into a local file on my computer (making sure the relevant lines are indented) and ran the program, I got the following output:

victoria@victoria-ixtreme-M5801 ~/Documents/Python $ python lumberjack.py What's your name? Kenneth Traceback (most recent call last): File "lumberjack.py", line 1, in <module> name = input("What's your name? ") File "<string>", line 1, in <module> NameError: name 'Kenneth' is not defined

I get the same error regardless of what name I type at the "What is your name?" prompt (although obviously whatever name you type gets substituted in the error message).

I have my editor configured so that it indents 4 spaces when I press the Tab key - is this correct?

4 Answers

Kenneth Love
STAFF
Kenneth Love
Treehouse Guest Teacher

You're on Python 2.7! Python 2 had a really bad bug (it was designed but I say it's a bug) when you use input() where it takes the string that comes in and tries to run it as live Python code. This was fixed in Python 3, so I definitely encourage you to upgrade if you can. If you can't/won't, use raw_input() instead like Dan Johnson pointed out.

Dan Johnson
Dan Johnson
40,532 Points

If you're running an older version of Python you'll need to either surround the input in quotes or use raw_input:

name = raw_input("Enter your name: ")
Victoria Holland
Victoria Holland
6,159 Points

Thanks for your replies. Substituting input with raw_input worked for me.

However, I have a query about the different Python versions. I'm using Linux Mint 17, and I have both Python 2.7.6 and 3.4.0 pre-installed. According to what I've read online, v2.7.6 must always remain the default version, as that's what the system is built on. However, I read some instructions saying that you can force the usage of the more recent version of Python on a script-by-script basis, by adding the shebang code to the top of the script file, pointing to the version of Python you want to use.
When I typed "which python3.4" into the terminal, it told me that Python 3.4 is located at /usr/bin/python3.4 So, I then tried adding #!/usr/bin/python3.4 as the first line of the lumberjack.py file, and I changed raw_input back to input and resaved the file. But when I tried re-running the script, I still got the same error message as before. So it seems like my system is still using Python 2.7, even though I told it to use 3.4 just for this script. Can anyone confirm how I can force it to use version 3.4 for this script? However, I've noticed it seems to work OK if I run the script using the command python3.4 lumberjack.py

Kenneth Love
Kenneth Love
Treehouse Guest Teacher

When you ran it, I'll guess you did python lumberjack.py. That ignores the shebang and runs it with the version of Python you told it to. In this case, Python 2.7.

You can chmod +x <your file> and then just execute it with ./lumberjack.py or do what you've done and used python3.4 lumberjack.py to control the version at will.

Victoria Holland
Victoria Holland
6,159 Points

Thanks, I was indeed using python lumberjack.py to run the script. I'm now using the ./lumberjack.py method and it works as expected. Thanks for the explanation!