+ 2
Boolean Problem
Please help me :( https://code.sololearn.com/cD3Yqn2TlL4x/?ref=app
5 ответов
+ 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())))
+ 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)
+ 2
Thank you Everyone!😁
+ 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"
+ 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.