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

Gary Gibson
Gary Gibson
5,011 Points

Why is "else" giving me a Syntax Error while "elif" works just fine?

Here is my script:

def add_item():
    while True:
        item = input("Enter your item here. > ")    

        if item == 'DONE':
            break

        elif item == 'SHOW':
            SHOW()
            continue # without this continue, SHOW is added to the shopping list!

        elif item == 'HELP':
            HELP()
            continue # without this continue, HELP is added to the list.

If I try to make that last "elif" into an "else", I get this:

File "try_again_1.py", line 28
else item == 'HELP':
^
SyntaxError: invalid syntax

3 Answers

Hi Gary,

The 'else' clause is optional, and therefore doesn't take any conditional statements, as do the 'if' and 'elif' clauses/keywords. It's meant as a catchall - if none of the above if/elif conditions are met, then do this other thing.'

Hope that clarifies things!

deckey
deckey
14,630 Points

Hi Gary, you can not state else with a condition, like else item == 'HELP'. Instead just use else{} statement

hope it helps, good luck!

Gary Gibson
Gary Gibson
5,011 Points

Okay, I see.

I was able to add:

else:

print("Okay, I'll add that to the list!")

I can't have "else" followed by a condition that needs a introductory condition (if) of some kind. "Else" is all the rest. Got it.