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
Ryan Wycoff
Courses Plus Student 1,037 PointsCode not working properly
I decided to get adventurous and see if I really grasped what little I've been taught so far in the Python course before proceeding, so I decided to make this little program.
store_open = None
store_hours = [8, 9, 10, 11]
time = input()
if time in store_hours:
store_open = True
print("Come on in!")
else:
store_open = False
print("Sorry, we're closed")
The problem is, even if I input a number inside the store hours list, I still get "Sorry, we're closed."
Any advice on what I'm missing here would be greatly appreciated!
2 Answers
Chris Freeman
Treehouse Moderator 68,468 PointsIn Python 3, the input() function returns a string. This will never match the integers in store_hours. The input needs to be converted to an int:
store_open = None
store_hours = [8, 9, 10, 11]
time = int(input("> ")) # <-- added prompt and convert to int
if time in store_hours:
store_open = True
print("Come on in!")
else:
store_open = False
print("Sorry, we're closed")
Andreas cormack
Python Web Development Techdegree Graduate 33,011 PointsHi Ryan
Convert time to an Integer first. Because the way you code is written its looking for a string in the list rather than an integer.
store_open = None
store_hours = [8, 9, 10, 11]
time = int(input()) # converting input to an integer
if time in store_hours:
store_open = True
print("Come on in!")
else:
store_open = False
print("Sorry, we're closed")