-1 python
我一直在CodingBat上练习,我被困在Logic-1> alarm_clock问题上.这是我的代码
def alarm_clock(day, vacation):
if 0 < day < 6 and not vacation:
return "7:00"
elif day == 0 or day == 6 and not vacation:
return "10:00"
elif day == 6 or day == 0 and vacation:
return "off"
else:
return "10:00"
Run Code Online (Sandbox Code Playgroud)
但是,该网站告诉我,我的代码不正确. 这是网站
你的问题在这里(并重复下一个条款):
elif day == 0 or day == 6 and not vacation:
Run Code Online (Sandbox Code Playgroud)
在Python中(遵循大多数早期的计算机语言)and具有更高的优先级or.在以同样的方式2 + 3 * 5手段2 + (3*5),而不是(2+3) * 5,你的代码并不意味着(day==0 or day==6) and not vacation,它的意思day==0 or (day==6 and not vacation).
要解决此问题,只需添加显式括号(在两个子句中).
但请注意,你已经day == 0 or day == 6在一条线上,而day == 6 or day == 0另一条线,但这些实际上是相同的条件.所以你可以简化一下:
elif day == 0 or day == 6:
if not vacation:
return "10:00"
els:
return "off"
Run Code Online (Sandbox Code Playgroud)
......然后问题甚至没有出现.
| 归档时间: |
|
| 查看次数: |
78 次 |
| 最近记录: |