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

Else 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
STAFF
Kenneth Love
Treehouse Guest Teacher

Looking 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
Stone Preston
42,016 Points

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

Are there any other errors? I've redone this about 5 times with this new answer and it still isn't working :(

Stone Preston
Stone Preston
42,016 Points

woops. forgot python comments use # instead of //. fixed it in my answer. try removing the comment and see if it works then

I left the text out, I don't know if that is the problem?

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

I had the same problem with the indentation and saving within the console. thanks for the assistance @Rusian

Stone Preston
Stone Preston
42,016 Points

it should not have anything to do with your OS. you most likely have an error in your code. can you post your code?

name = 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>

Are there any other possibilities? I cannot get over this :(

Stone Preston
Stone Preston
42,016 Points

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