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 trialValgeir Þórarinsson
209 PointsInvalid syntax on else:
The workspace points out that there's a syntax error on my else: statement, it looks like this:
File "lumberjack.py", line 5
else:
^
SyntaxError: invalid syntax
My code looks like this:
name = input("What's your name? ")
if name == "Valgeir": print(name + " is a lumberjack and he's OK!" else: print(name + " sleeps all night and " + name " works all day!")
Am I missing something obvious?, I wrote the code exactly like in the video, except I switched the name with my own.
2 Answers
James Simshaw
28,738 PointsHello,
It looks like you forgot to close your parenthesis on the previous line.
Lucas Rim
1,705 PointsHey man, I think I know what your problem is. You are actually running this program from inside python itself, go ahead and go type in quit(), then type python lumberjack.py , that should solve it my man!
Bryce Hatch
83 PointsThis works! I was having the same problem and quit() python fixed it.
Valgeir Þórarinsson
209 PointsValgeir Þórarinsson
209 PointsChanged it and I still get the same error:
name = input("What's your name? ")
if name == "Valgeir": print(name + " is a lumberjack and he's OK!") else: print(name + " sleeps all night and " + name " works all day!")
James Simshaw
28,738 PointsJames Simshaw
28,738 PointsOk, I missed a second error. In your line with
print(name + " sleeps all night and " + name " works all day!")
you forgot a + after the second name to concatenate the two strings. If there are more errors, it might be indentation which I cannot see from your posts.
Valgeir Þórarinsson
209 PointsValgeir Þórarinsson
209 PointsYeah, I've fixed that too and I am still getting the error, how would I fix an indentation?
James Simshaw
28,738 PointsJames Simshaw
28,738 PointsIn Python, indentation is very important since it tells Python where blocks end. If everything else is fixed, it should look like
Where there is an indentation for each of the code blocks, but the else is at the same indentation level as the if.
Valgeir Þórarinsson
209 PointsValgeir Þórarinsson
209 PointsThank you, but I think the problem is with the workspace, I'm still getting the error there, but using IDLE worked fine, I guess I'll just use that instead.