Boolean Problem | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
15th Sep 2021, 4:24 PM
Marzan
Marzan - avatar
5 Answers
+ 2
Hi Marzan! I assume that talking and hour parameters are input variables. Here, the main thing you have to consider is that operator precedence. and operator has higher precedence than or and they are evaluated left to right. Inorder to handle this you can use parenthesis. So, it will check both parameters separately. Here it is your corrected code. def parrot_trouble(talking, hour): return talking and (hour < 7 or hour > 20) print(parrot_trouble(input(), int(input())))
15th Sep 2021, 4:44 PM
Python Learner
Python Learner - avatar
+ 3
Here the solution for one case. Have a look at the brackets! talking = True hour = 21 if talking == True and ( hour < 7 or hour > 20 ): print (True)
15th Sep 2021, 4:41 PM
Coding Cat
Coding Cat - avatar
+ 2
Thank you Everyone!😁
15th Sep 2021, 5:05 PM
Marzan
Marzan - avatar
+ 1
Use parenthesis when needed. First example: parrot_trouble(False, 21) talking == True -> False and hour > 7 already false because previous argument is False or hour > 20 here is where its true, because hour is 21 and due to the "or" that's all that's necessary to be true to satisfy the condition if talking == True and (hour < 7 or hour > 20): fixes this, now the "or" is tied to the "and"
15th Sep 2021, 4:40 PM
Josiah Mathieu
Josiah Mathieu - avatar
+ 1
Next thing: You must only once check if talking is false. In this case you never had a problem. So return False. After that is clear, that talking must be true. So you then only have to check the time.
15th Sep 2021, 4:48 PM
Coding Cat
Coding Cat - avatar