Python中while循环一直不停止

3 投票
3 回答
5278 浏览
提问于 2025-04-18 03:40

我们正在尝试制作一个Python程序,用来模拟掷骰子游戏(craps),并显示获胜的概率。我发现问题出在一个循环上,正如标题所说,当程序进入这个循环时,它不会退出。根据我的观察,这个循环应该是可以退出的,但结果却不断返回“false”。下面是整个程序的代码和注释,我会在最后写出我们程序使用的掷骰子游戏规则。

import random
def craps()
    roll = random.randint(1,6) + random.randint(1,6)
if (roll == 7 or roll == 11): #Tests if the player should win. If so it adds a 1 to the win counter 
    return 1
elif (roll == 2 or roll == 3 or roll == 12): #Tests if player should lose. If so adds 0
    return 0
else:
    roll2 = 0 #initializes roll2 for the while
    while (roll2 != roll or roll2 != 7): #The player keeps rolling until they get a 7 or the initial roll
        roll2 == random.randint(1,6) + random.randint(1,6) #Rolls the dice
        if (roll2 == roll): #Tests if the player should win
            return 1
        elif (roll2 == 7): #Tests if the player should lose
            return 0

win = 0 #Start the win counter at 0
games = int(input("Enter the amount of games you want played: ")) #Accept how many games will be played
for i in range (games): 
    win = win + craps() #adds a 1 or a 0 depending on if a game was won

print ("The probability of winning is:", win, "/", games, " =", float(win)/games) #print the probability of winning

我们使用的基本规则是:玩家掷两个六面骰子,如果第一次掷出的点数是7或11,玩家就赢了(第4行)。如果玩家掷出的是2、3或12,玩家就输了(第6行)。如果玩家没有掷出这几种点数,他们会继续掷骰子,直到掷出和第一次相同的点数或者掷出7。如果掷出的点数和第一次相同,玩家就赢了;如果掷出7,玩家就输了(第10到15行)。我们的程序是用来模拟这个过程并显示玩家获胜的概率。

这是我第一次使用这个网站,如果我有什么做得不对的地方,请告诉我。谢谢你的帮助!

3 个回答

1

你第一个问题出在这一行:

while (roll2 != roll or roll2 != 7): #The player keeps rolling until they get a 7 or the initial roll

如果roll不等于7,这段代码会变成一个无限循环。你这里应该用一个and,因为你想要在其中一个条件为false时改变,而不是在两个条件都为false时。明白了吗?

你想要的是:

while (roll2 != roll and roll2 != 7): #as long as both are not `True` then keep rolling.

你第二个问题出在这一行:

roll2 == random.randint(1,6) + random.randint(1,6) 

双等号是用来检查roll2是否等于左边的表达式。你想做的是把它设置为左边的表达式,像这样:

roll2 = random.randint(1,6) + random.randint(1,6) 
2

双等号是用来检查两个东西是否相等的。如果你想让你的代码正常工作,把它改成单等号就可以了。以下是你修改后的代码:

import random
def craps()
    roll = random.randint(1,6) + random.randint(1,6)
if (roll == 7 or roll == 11): #Tests if the player should win. If so it adds a 1 to the win counter 
    return 1
elif (roll == 2 or roll == 3 or roll == 12): #Tests if player should lose. If so adds 0
    return 0
else:
    roll2 = 0 #initializes roll2 for the while
    while (roll2 != roll or roll2 != 7): #The player keeps rolling until they get a 7 or the initial roll
        roll2 = random.randint(1,6) + random.randint(1,6) #Rolls the dice
        if (roll2 == roll): #Tests if the player should win
            return 1
        elif (roll2 == 7): #Tests if the player should lose
            return 0

win = 0 #Start the win counter at 0
games = int(input("Enter the amount of games you want played: ")) #Accept how many games will be played
for i in range (games): 
    win = win + craps() #adds a 1 or a 0 depending on if a game was won

print ("The probability of winning is:", win, "/", games, " =", float(win)/games) #print the probability of winning

这里有一个例子:

>>> import time
>>> x = 7
>>> x == 7
True
>>> while x != 6:
...     x == 6
...     time.sleep(1)
... 
False
False
False
^CTraceback (most recent call last):
  File "<stdin>", line 3, in <module>
KeyboardInterrupt
>>> while x != 6:
...     x = 6
...     time.sleep(1)
... 
>>> 
3

一个常见但非常让人头疼的错误,甚至连最优秀的Python开发者也会犯。把 roll2 == random.randint(1,6) + random.randint(1,6) 改成 roll2 = random.randint(1,6) + random.randint(1,6)

另外,我觉得你需要 roll2 != roll and roll2 != 7。从风格上来说,在 ifwhile 语句中,最好不要把条件用括号括起来。

不过,你的循环里有一点多余的部分。你在每次循环的开始和结束都检查 roll2 是否等于 roll 或者 7。你也可以考虑试试这个:

while True:
    # do everything in the same way as before

或者

while roll2 != roll and roll2 != 7:
    # do stuff with the roll
    roll = random.randint(1,6) + random.randint(1,6)
return 1 if roll2 == roll else return 0 # a "ternary operator" in Python

撰写回答