【发布时间】:2019-08-27 09:50:56
【问题描述】:
我不能清楚地使用随机 int 作为变量来在我的代码中使用它进行比较。无论产生的随机数如何,似乎都只会抛出第一个 if 语句。
from random import randint
rate = randint(0, 255)
wind = rate
print(rate)
if wind > 251:
print("The hurricane is a category 5!")
elif wind <= 251 or wind >= 209:
print("The hurricane is a category 4!")
elif wind <= 208 or wind >= 178:
print("The hurricane is a category 3!")
该代码假设使用范围内的随机数来确定飓风等级并打印有关的消息。代码在一定程度上有效.. 无论脚本生成的随机数如何,都没有错误消息总是打印类别 4 行
【问题讨论】:
-
用
ands替换你的ors