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

a2p
a2p
1,624 Points

Error - lumberjack.py

I have this lumberjack.py code

name = input("What's your name? ")

if name == "Kenneth":
    print(name + "is a lumberjack and he's OK")
else:
    print("{} sleeps all night and {} sleeps all day".format(name,name))

If I test it into treehouse workspace everything goes fine but If I put exactly the same code into sublime text 2 and I run the file in its console I am getting this error

>>> python lumberjack.py
  File "<string>", line 1
    python lumberjack.py
                    ^
SyntaxError: invalid syntax

In Mac OS terminal it asks my name but when I input it I am getting an error too

name = input("What's your name? ")
  File "<string>", line 1, in <module>
NameError: name 'Kenneth' is not defined

I am a newbie but I would like to run my code outside your platform, I guess it is the final purpose of doing these kind of courses.

Any suggestion is appreciated. Thank you and good job with the platform. It's really great.

If you are using Python 2.x then use raw_input() instead of input().

2 Answers

If you are using an outside editor like Sublime you have to be in the right build. Get into Sublime and go to Tools and then see Build System (here is a screen shot: http://cl.ly/image/1q3R051x2I34).

Change it to Python and then the code should work because it will recognize that it is for the Python language.

Kenneth Love
STAFF
Kenneth Love
Treehouse Guest Teacher

I have a feeling you're running the code against Python 2 and not Python 3. We're using Python 3 here at Treehouse and the two versions of the language have a few differences. For one of your examples, input() in Python 2 actually preforms an eval(), or tries to turn the input into actual Python code. That's why it says "no variable named Kenneth".

Shouldn't this be mentioned in the course description so we know what it is we are learning? (Sorry physics student using vPython 6 with no Python 3 compatibility here)

Kenneth Love
Kenneth Love
Treehouse Guest Teacher

@Kristoffer Hess Sorry it wasn't made clear that we were teaching Python 3 instead of 2. You can still write all the Python 3 code you want in our Workspaces.

Also, I'm not familiar with "vPython 6". Can you tell me more about that?