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 Membership

Erik Luo
Erik Luo
3,810 Points

Invalid syntax

here is the task: I'm going to create a variable named time. It'll be an integer for the current hour (well, what I want the current hour to be). I need you to make an if condition that sets store_open to True if time is in store_hours. If time isn't in store_hours, set store_open to False. You'll probably have to use if, else, and in to solve this one. I don't know what's wrong, and can you tell me why it got invalid syntax for line 5.

membership.py
store_open = None
store_hours = [9, 10, 11, 12, 13, 14, 15, 16, 17, 18]
if time in store_hours:
    store_open = True
    else time not in store_hours:
        store_open = False

3 Answers

Jones Dias
Jones Dias
13,902 Points

Erik, it doesn't work because else can't have conditions. So to use a condition, you should use elif (that means else if).

Jones Dias
Jones Dias
13,902 Points

Hello, Erik!

You're with a indentation error and a syntax one. Instead of using else time not in.. you should use elif. Your code should be like this:

store_open = None
store_hours = [9, 10, 11, 12, 13, 14, 15, 16, 17, 18]
if time in store_hours:
    store_open = True
elif time not in store_hours:
    store_open = False

Pay attention on the errors you got, it'll turn you into a better programmer.

Keep coding.

Erik Luo
Erik Luo
3,810 Points

That worked, can you tell me why else doesn't work?

changed comment to answer

It's ok to use else here and it's easier to do it that way. The problem is that you can't have a condition on it.

else:
    store_open = False
Erik Luo
Erik Luo
3,810 Points

I get it now, thank you.

Jones Dias
Jones Dias
13,902 Points

You're more than welcome, man!