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 trialLindsey Davis
2,301 PointsElse error on lumberjack.py
I keep receiving an error that is associated with else: . Could that be because I'm using a Mac? I have the exact work as Kenneth does.
5 Answers
Kenneth Love
Treehouse Guest TeacherLooking at the code you replied to Stone with, you do need to dedent your else:
and the block following in, so the else:
lines up with the if
and the code inside the else
is just one tab in. I don't see any other syntax errors.
Stone Preston
42,016 Pointsyour else block needs to be unindented so its outside the if block. right now its inside the if block
name = input("What's your name? ")
if name == "Kenneth":
print(name + " is a lumberkack and he's okay!")
#unindent it the else block so its outside of the if block
else:
print(name + " sleeps all night and " + name + " works all day!")
Lindsey Davis
2,301 PointsAre there any other errors? I've redone this about 5 times with this new answer and it still isn't working :(
Stone Preston
42,016 Pointswoops. forgot python comments use # instead of //. fixed it in my answer. try removing the comment and see if it works then
Lindsey Davis
2,301 PointsI left the text out, I don't know if that is the problem?
ruslanfarutdinov
198 PointsI had the same problem. Definitely make sure that else: and if blocks are lined up. But also, after writing code save the code (command + S) and then run it- that is what made it work for me. It's almost like, otherwise python doesn't recognize that you've made changes.
ben rodriguez
6,382 PointsI had the same problem with the indentation and saving within the console. thanks for the assistance @Rusian
Stone Preston
42,016 Pointsit should not have anything to do with your OS. you most likely have an error in your code. can you post your code?
Lindsey Davis
2,301 Pointsname = input("What's your name? ")
if name == "Kenneth":
print(name + " is a lumberkack and he's okay!")
else:
print(name + " sleeps all night and " + name + " works all day!")
CONSOLE:
<pre><code>
treehouse:~/workspace$ python lumberjack.py
File "lumberjack.py", line 4
else:
^
SyntaxError: invalid syntax
treehouse:~/workspace
</code></pre>
Lindsey Davis
2,301 PointsAre there any other possibilities? I cannot get over this :(
Stone Preston
42,016 Pointsjust tried using the following code after fixing the indentation and it worked fine
name = input("What's your name? ")
if name == "Kenneth":
print(name + " is a lumberkack and he's okay!")
#unindent it the else block so its outside of the if block
else:
print(name + " sleeps all night and " + name + " works all day!")