2 回答
TA贡献1805条经验 获得超9个赞
您的范围检查并不严格,例如:包括andrange(2, 5)之间的整数,但无论如何有一种更简单的方法来编写它;尝试这个:24
import random
n = random.randint(1,100)
if n % 2 != 0:
print("Weird")
elif 2 <= n <= 5: # from this point on, the number can only be even
print("Not Weird")
elif 6 <= n <= 20:
print("Weird")
elif n > 20:
print("Not Weird")
或者更好的是,它更简单并且做同样的事情:
if n % 2 != 0 or 6 <= n <= 20:
print("Weird")
else:
print("Not Weird")
TA贡献1790条经验 获得超9个赞
随机导入
n = random.randint(1,100)
print(n)
if (n % 2 != 0):
print("Weird")
elif (n % 2 == 0 and n in range(2,5)):
print("Not Weird")
elif (n % 2 == 0 and n in range(5,20)):
print("Weird")
elif (n % 2 == 0 and n >= 20):
print("Not Weird")
您缺少一些范围,我刚刚更新了代码尝试一下。
添加回答
举报