语法练习:parrot_trouble
题目:parrot_trouble
We have a loud talking parrot. The “hour” parameter is the current hour time in the range 0…23. We are in trouble if the parrot is talking and the hour is before 7 or after 20. Return True if we are in trouble.
parrot_trouble(True, 6) → True
parrot_trouble(True, 7) → False
parrot_trouble(False, 6) → False
我的解答:
def parrot_trouble(talking, hour):
if talking&(hour < 7 or hour > 20):
return True
else:
return False
Expected Run
parrot_trouble(True, 6) → True True OK
parrot_trouble(True, 7) → False False OK
parrot_trouble(False, 6) → False False OK
parrot_trouble(True, 21) → True True OK
parrot_trouble(False, 21) → False False OK
parrot_trouble(False, 20) → False False OK
parrot_trouble(True, 23) → True True OK
parrot_trouble(False, 23) → False False OK
parrot_trouble(True, 20) → False False OK
parrot_trouble(False, 12) → False False OK
All Correct
标答:
def parrot_trouble(talking, hour):
return (talking and (hour < 7 or hour > 20))
# Need extra parenthesis around the or clause
# since and binds more tightly than or.
# and is like arithmetic *, or is like arithmetic +