语法练习:parrot_trouble

本文解析了一道关于判断鹦鹉是否在特定时间段内造成麻烦的编程题目。通过定义函数parrot_trouble来检查鹦鹉在指定的时间段内是否正在说话,并据此返回相应的布尔值。该函数考虑了鹦鹉说话的时间限制条件。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

语法练习: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 +
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值