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 trialAndreas Wassberg
5,856 PointsWhat am I doing wrong with this code challenge? If, else and in.
Whats wrong with my code?
Heres the question:
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.
store_open = None
store_hours = [9, 10, 11, 12, 13, 14, 15, 16, 17, 18]
time = 9
if time in store_hours:
store_open = True
else:
store_open = False
[MOD: added ```python markdown formatting -cf]
Stephen Chisa
9,849 PointsI just want to +1 @nekilof's comment on having the grader add code.
4 Answers
jag
18,266 Pointsstore_open = None
store_hours = [9, 10, 11, 12, 13, 14, 15, 16, 17, 18]
time
if time in store_hours:
store_open = True
else:
store_open = False
You aren't supposed to set the integer for time, leaving it blank will allow you to pass.
Andreas Wassberg
5,856 PointsThanks! It worked. But why am I not supposed to set time to a Int? Time isn't in store_hours.
Chris Freeman
Treehouse Moderator 68,441 PointsThe clue is in the line "I'm going to create a variable named time
." This means that the grader will create the variable and set it to some value. You won't know what value it chooses, and may run your code with various values for time
so that it can test both logical paths through the if
statement.
The solution would also be correct by removing the time
line completely.
Chunjie Nan
2,825 Pointsthe challenge is not easy
doctacloak
12,202 PointsYou just want to make sure that you pay attention to details. For instance, if I were to tell you that I am going to store five socks within the box, and requested that you conduct an action if and only if those five socks were in the box, and you added additional socks to the box, it would void the task that I gave to you. One reason could be because that container was meant to go to another individual, and perhaps having an additional x amount of socks could potentially cause an unwanted issue of occurrence.
Edwin Castro
Courses Plus Student 24,037 PointsI understood the whole lesson on 'IN' but couldn't get this challenge either, thank you!
doctacloak
12,202 PointsNo worries mate, glad we could help! :)
Antonio De Rose
20,885 Points@Andreas Wassberg, nothing wrong with the code, in my view, and I tested the same code, in the python shell, by launching the workspace, it returns true, anyhow, given the scenario, for this very objective, it is meant to be left blank.
A X
12,842 PointsA X
12,842 PointsI am disliking the challenges that the "grader" is adding in things that we're not. It muddles and confuses things, as then we can't see the full logic behind why something does/doesn't work. Andreas, you're totally correct, we should've needed to change the values to int, but we can't see what invisible things the "grader" is setting. Really frustrating code challenge design.