python - 没有跳出 while 循环

标签 python

<分区>

我编写了一些代码,可以掷两个骰子并询问用户多少次。之后它会询问用户是否要再次玩。由于某种原因,它没有在应该跳出的 while 循环中跳出。

代码如下:

import random
again = True
rolls = 0
while again == True:
    rolls = float(raw_input("How many times would you like the dice to roll?"))
    while rolls >= 1:
        dice1 = random.randint(1,6)
        dice2 = random.randint(1,6)
        print dice1 , dice2
        rolls = rolls - 1
    again = raw_input("Would you like to play again?")
    if again == "Yes" or "Y" or "yes" or "y":
        again = True
    else:
        again = False

谁能帮帮我

最佳答案

问题是这一行:

if again == "Yes" or "Y" or "yes" or "y":

计算结果为:

if (again == "Yes") or "Y" or "yes" or "y":

换句话说,它总是正确的。你的意思是:

if again in ("Yes", "Y", "yes", "y"):

关于python - 没有跳出 while 循环,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20047347/

相关文章:

python - flake8/pylint 在 Tox 测试环境中失败,引发 InvocationError

python - 返回对象指针 : 'Cannot convert ' Coordinate *' to Python object'

python - 在父类(super class)构造函数中的子类中添加方法

python - 在seaborn中旋转标签文本

python - python 对列表中的两个整数求和

python - 在 Python 中跟踪数据类型

Python 箭头添加小时/分钟/等 vs 替换

Python etree控件空标签格式

python - 创建一个新列,根据条件查找日期差异

python - py2app 处方