这是一个更大问题的一部分,但我在使用 if/elif 函数时遇到了一些问题。
def fish():
import random
score = 0
i = random.randint(0,39)
if i == [0,19]:
print("You caught nothing!")
elif i == [20,39]:
print("You caught a Minnow! +10 points.")
score += 10
print(i)
print(score)
fish()
当我运行它时,我得到的只是随机数,0 表示分数。我不确定我在这里做错了什么。
最佳答案
是的,嗯……事情不是这样的。您正在将整数与列表进行比较。
if 0 <= i < 20:
print("You caught nothing!")
elif 20 <= i < 40:
print("You caught a Minnow! +10 points.")
score += 10
关于Python if/elif 问题与 random.randint,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8109288/