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 Python Basics (Retired) Ins & Outs String Concatenation

Valgeir Þórarinsson
Valgeir Þórarinsson
209 Points

Invalid 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

Hello,

It looks like you forgot to close your parenthesis on the previous line.

Valgeir Þórarinsson
Valgeir Þórarinsson
209 Points

Changed 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!")

Ok, 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
Valgeir Þórarinsson
209 Points

Yeah, I've fixed that too and I am still getting the error, how would I fix an indentation?

In Python, indentation is very important since it tells Python where blocks end. If everything else is fixed, it should look like

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!")

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
Valgeir Þórarinsson
209 Points

Thank 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.

Lucas Rim
Lucas Rim
1,705 Points

Hey 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
Bryce Hatch
83 Points

This works! I was having the same problem and quit() python fixed it.