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 (2015) Logic in Python Conditional Value

Christian Geib
Christian Geib
826 Points

error message Bummer! invalid syntax (<string>, line 5) even though code appears to work

Hi guys, with the help of some of you I have been able to come up with some code that does work. However, when I click on Check Work I still receive the following error message:

Bummer! invalid syntax (<string>, line 5) even though code appears to work

conditions.py
admitted = None 
if age >= 13: 
    admitted = True 
    else: 
        admitted = False

2 Answers

Michael Hulet
Michael Hulet
47,913 Points

Python associates blocks of code based on how much they're indented. Furthermore, every else keyword needs a corresponding if keyword to go before it. That means that for every else, there must be an if statement before it that's indented at the same level. In your code, the else and line within it is indented farther than the if block above, so Python sees it as an entirely different construct that it doesn't recognize. If you put the else on the same indentation level as the if (and make sure the code inside the else is also indented how it should be), the syntax error will go away.

Christian Geib
Christian Geib
826 Points

Thanks for the quick, elaborate and illuminating response. That did the trick:-)

Christian Geib
Christian Geib
826 Points

Thanks for the quick and most helpful response. That did the trick:-)