Boolean Logic (debug) | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

Boolean Logic (debug)

Hey guys! can you please help me debug my code and explain why it has bug? ##the shop is Open around 10 to 21 o'clock. it opens only Monday to Friday except SAT and SUN. (the day of the week represented as 1 for Mon, 2 for Tue, etc.) #this is my code hour = int(input()) day = int(input()) if(hour >= 10 or hour < 21): if(day == 6 or day == 7): print("Closed") else: print("Open") # but the result goes like this: 9 1 Open 22 2 Open

7th Jan 2021, 1:59 PM
Yamo
Yamo - avatar
20 Answers
+ 9
And also, add an another else statement outside the nested if-else so if the first condition is False, it will print "Closed" . hour = int(input()) day = int(input()) if(hour >= 10 and hour < 21): if(day == 6 or day == 7): print("Closed") else: print("Open") else: print("Closed")
7th Jan 2021, 2:08 PM
noteve
noteve - avatar
+ 10
#Change your first condition #Everything will be fine #Try this hour = int(input()) day = int(input()) if(hour <= 10 or hour > 21) or (day == 6 or day == 7): print("Closed") else: print("Open")
7th Jan 2021, 3:45 PM
Simba
Simba - avatar
+ 5
9 is less than 21 so it meets that check. You should use AND instead of OR in the check (or just shorten to: if 10 <= hour < 21)
7th Jan 2021, 2:02 PM
Slick
Slick - avatar
+ 5
Yamo Anyways now that the problem is solved, let us just be thankful to those who helped as they enable us to solve it. ☺👍
7th Jan 2021, 4:07 PM
noteve
noteve - avatar
+ 4
thank you guys! i should practice more in boolean logic
7th Jan 2021, 2:34 PM
Yamo
Yamo - avatar
+ 4
Yeah thank you very much guys! I want to enhance my coding skils more in boolean logic
7th Jan 2021, 4:08 PM
Yamo
Yamo - avatar
+ 3
Slick Oh, I got wrong in that part, Thanks for that correction!
7th Jan 2021, 3:47 PM
noteve
noteve - avatar
+ 2
yeah cause 6 doesnt equal 7. your check only passes if the number is equal to 6 AND 7, wich is impossible this is where you'd use OR
7th Jan 2021, 3:15 PM
Slick
Slick - avatar
+ 2
Just use the first one you had that worked after the tweak. It was more readable anyways
7th Jan 2021, 4:02 PM
Slick
Slick - avatar
+ 2
Here the complete program using IntEnum machinery for validation and operate. Also importable as module https://code.sololearn.com/cdUXQjFbBXhS/?ref=app
9th Jan 2021, 9:10 PM
David Ordás
David Ordás - avatar
+ 1
Thank you! This is very confusing 😃
7th Jan 2021, 3:22 PM
Yamo
Yamo - avatar
+ 1
Simba that's no output
7th Jan 2021, 3:51 PM
Yamo
Yamo - avatar
+ 1
Simba 13 6 Open #fail
7th Jan 2021, 3:57 PM
Yamo
Yamo - avatar
+ 1
slick already help me to debug Simba
7th Jan 2021, 3:57 PM
Yamo
Yamo - avatar
+ 1
That one uses OR so if one passes, it moves on. You need them both to pass, so use AND
7th Jan 2021, 3:59 PM
Slick
Slick - avatar
+ 1
Simba hahahaha😆
7th Jan 2021, 3:59 PM
Yamo
Yamo - avatar
+ 1
Slick but it requires a conditional input so what is best for it?
7th Jan 2021, 4:01 PM
Yamo
Yamo - avatar
7th Jan 2021, 4:58 PM
Slick
Slick - avatar
0
hour = int(input()) day = int(input()) if(hour >= 10 and hour < 21): if(day == 6 and day == 7): print("Closed") else: print("Open") else: print("Closed") #but 13 6 Open #there's more fail on my code
7th Jan 2021, 3:13 PM
Yamo
Yamo - avatar
0
hour=int(input()) day=int(input()) if(hour>=10 or hour<21): if(day>=1 or day<=5): print("open") else: print("closed")
8th Jan 2021, 3:33 PM
Vallamkondu Divya
Vallamkondu Divya - avatar