0

Boolean logic confused, please help me guys

Please look this question and help me to solve it, because i'm so confused to what this app want. Or it just me who didn't understand after all? I need to create a program that outputs whether a store is Open or Closed, based on the hour and the day of the week. The store is open on all days from 10 to 21, except Saturday and Sunday. I need to take the hour and the day of the week as input. The day of the week is represented as an integer (1 for Monday, 2 for Tuesday, etc.) Sample Input: 15 4 Sample Output: Open I try to answer those question with this: hour = int(input()) day = int(input()) if (day==1 and hour>=10 and hour<=21):print("Open") elif (day==2 and hour>=10 and hour<=21):print("Open") elif (day==3 and hour>=10 and hour<=21):print("Open") elif (day==4 and hour>=10 and hour<=21):print("Open") elif (day==5 and hour>=10 and hour<=21):print("Open") elif (day==6 or day==7):print("Closed") #why_it_still_wrong_???

2nd Dec 2020, 3:14 PM
Novian
Novian - avatar
12 Answers
+ 10
Novian , may be it's worth to re-think about the way you check the day and the time. There is some duplication in code that can be reduced. Instead of using several if - elif conditions, you could use range() to define the valid time the shop is open, the same can be done for the day. Doing so, you need only one if - else condition to check. Then the membership operator "in" can be used to check if day and time is in range. keep in mind that using range, the end parameter is not included. range(1,5) does create numbers 1,2,3,4. Happy coding!
2nd Dec 2020, 4:20 PM
Lothar
Lothar - avatar
+ 9
This is what your completed code hour = int(input()) day = int(input()) if (day==1 and hour>=10 and hour<=21):print("Open") elif (day==2 and hour>=10 and hour<=21):print("Open") elif (day==3 and hour>=10 and hour<=21):print("Open") elif (day==4 and hour>=10 and hour<=21):print("Open") elif (day==5 and hour>=10 and hour<=21):print("Open") elif (day==6 or day==7):print("Closed") elif hour<10 or hour>21:print("Closed") But you're able to do it like this after learning about range() as Lothar said hour = int(input()) day = int(input()) if hour in range(10,22) and day in range(1,6): print("Open") else: print("Closed")
3rd Dec 2020, 7:01 AM
Simba
Simba - avatar
+ 6
Fill in the blanks to output ”Closed” if the hour variable is larger than 20 or if the day variable is equal to ”Sunday”. answer:- if(hour > 20 or day == “Sunday”): print(“Closed”) else: print(“Open”)
24th Mar 2022, 5:26 PM
B.D.R.J.BOMBUWALA
+ 2
Simba thanks a lot mate, it works
5th Dec 2020, 6:51 AM
Novian
Novian - avatar
+ 1
Fill in the blanks to output ”Closed” if the hour variable is larger than 20 or if the day variable is equal to ”Sunday”. if(hour > ______or______== “Sunday”): print(“Closed”) else: print(“Open”) can help me about this task please..
13th Mar 2022, 5:24 AM
Sirhan Haikal
Sirhan Haikal - avatar
0
I try to answer like this, and still wrong: hour = int(input()) day = int(input()) if (day==1 and day==2 and day==3 and day==4 and day==5):print("Open") elif(hour>=10 and hour<=21):print("Open") elif (hour<=10 or hour>=21):print("Closed") elif(day==6 and day==7):print("Closed") else: print("Closed") # your code goes here
3rd Dec 2020, 6:19 AM
Novian
Novian - avatar
0
Lothar it is work with boolean logic? I mean mu course still stuck on boolean logic number 2. I didn't even know what is range() and how to use it.
3rd Dec 2020, 6:20 AM
Novian
Novian - avatar
0
Also trying to figure this out. My code works for all test cases but one: if day >= 1 and day <= 5: if hour > 9 and hour < 21: print("Open") else: print("Closed")
7th Dec 2020, 11:45 AM
Fast
0
here's a short one hour = int(input()) day = int(input()) if (10<=hour<=21) and (day<=5) : print("Open") else : print("Closed")
28th May 2021, 2:47 PM
Asmita Gharat
Asmita Gharat - avatar
0
This was my solution: hour = int(input()) day = int(input()) # your code goes here if hour in range(10,22): if day in range(1,6): print("Open") else: print("Closed") but test case 4 was the only one that was wrong... Then I tried Simba's solution and it was correct. Can anyone tell me what the issue might have been in my code?
14th Oct 2021, 4:54 PM
Joachim Gasser
0
its ans is 20 or day
4th Jan 2023, 3:17 PM
Engr.Ayesha Liaqat
Engr.Ayesha Liaqat - avatar