+ 2

Did I break the Boolean Logic 2 exam?

The code I used gets the expected results, but isn’t registering as a pass. What am I missing? Question Summary: If hour is between 10 and 21 AND day is between 1 and 5 then print Open, if not then print Closed. hour = int(input()) day = int(input()) if hour >= 10 and hour <= 21: if day >= 1 and day <= 5: print ("Open") else: print("Closed") Edit: I ran more tests, it consistently prints Open, but it doesn’t consistently print Closed. Any clues as to why?

29th Nov 2020, 9:43 PM
... ....
... .... - avatar
7 Respostas
+ 4
Oh, lol well you could just chain your conditions together like; if (hour >= 10 and hour <= 21) and (day >= 1 and day <= 5): Or better yet; if 10 <= hour <= 21 and 1 <= day <= 5:
29th Nov 2020, 10:21 PM
ChaoticDawg
ChaoticDawg - avatar
+ 4
It will only print Closed if the hour is 10-21 and the day is not 1-5. If hour is outside the range 10-21, there is no output.
29th Nov 2020, 9:59 PM
ChaoticDawg
ChaoticDawg - avatar
+ 4
Try using the range() function and placing the checks for both the hour and the day in the same if statement. if hour in range(10, 22) and day in range(1, 6): ... else: ...
29th Nov 2020, 10:10 PM
ChaoticDawg
ChaoticDawg - avatar
+ 3
That totally worked, thank you! But now I really broke it because the range() function hasn’t been taught yet, lol!
29th Nov 2020, 10:16 PM
... ....
... .... - avatar
+ 3
You can keep a check variable and use it to know if conditions evaluated to true. check=0 #by default if hour >= 10 and hour <= 21: if day >= 1 and day <= 5: check=1 now after both if statement you can check if check==1 or not and print open, closed accordingly.
30th Nov 2020, 4:03 AM
Gaurav Agrawal
Gaurav Agrawal - avatar
+ 2
Thank you! I suspect it’s because the “Closed” is only a statement of the IF contained on the “hours” stanza. I need to somehow place an Else that catches if no parametrs are matched. Sorry if i used the wrong terminology.
29th Nov 2020, 10:07 PM
... ....
... .... - avatar
+ 2
Awesome, that’s reassuring beause my first attempt looked similar to the first option you listed, but I was missing something. I’m grateful for the helpful advice!
29th Nov 2020, 10:57 PM
... ....
... .... - avatar