语法练习:parrot_trouble
创始人
2024-03-23 12:16:04

语法练习: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 Trueelse: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 +

相关内容

热门资讯

“志愿联盟”承诺向乌克兰提供安... 新华财经巴黎1月6日电(记者孙毅)援助乌克兰“志愿联盟”成员国与乌克兰、美国等国领导人或代表6日在巴...
经济日报:春节归乡路,不应成为... (来源:上观新闻)各大平台抢购春节火车票的流程里,黏着各种或明或暗的加价服务,一不留神就会被套路。春...
“含绿量”也是“含金量” 用电量有望创新纪录!2025年全国全社会用电量预计首超10万亿千瓦时,其中“含绿量”不断提升:全国新...
走进北京双年展,看见缤纷世界(... 来源:人民日报海外版观众参观第十届中国北京国际美术双年展。郭俊锋摄(人民图片)母与子(雕塑)绍坎·托...
美媒:AI可能让爵士乐重获流行... 来源:环球美国彭博社2025年12月26日文章,原题:为什么人工智能可能让人类爵士乐再次流行 作...