Python if/elif 问题与 random.randint

标签 python python-3.x

这是一个更大问题的一部分,但我在使用 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/

相关文章:

python - 使用python(通过请求或其他方式)检索下拉列表选择的html表

python - 使用 pytest.fixture 返回模拟对象的正确方法

python - python 2 和 3 中的 __float__ 和 __round__

python - 为什么用 python3 用正确的逻辑计算数字中的数字会出现错误的答案?

Python 减去 float

python - 如何使用正则表达式替换一定数量的空格?

Python 散点图 - 如何查看每个点的条目数

Python,从生成器中生成的紧凑方法

python-3.x - 修改全局变量的NameError

python-3.x - 类型错误 : 'NoneType' - Selection Sort on Python