虽然循环不会在python中停止

时间:2014-04-22 02:35:19

标签: python random while-loop

我们正在尝试制作一个python程序来模拟掷骰子游戏并显示获胜的可能性。我已经缩小了while循环的答案,正如标题所示,当它击中循环时它不会退出。据我所知,循环应该退出,而只是返回和#34; 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

我们使用的基本规则是:玩家掷出2个六面骰子,如果初始掷骰是7或11,则玩家获胜(第4行)。如果玩家掷出2,3或12,则玩家输了(第6行)如果玩家没有得到任何一个他们继续滚动直到他们匹配初始滚动或滚动7.如果他们匹配初始滚动他们赢了,如果他们得到7他们输了(第10-15行)。我们的程序应该模拟并显示玩家获胜的概率。

这是我第一次使用这个网站,所以如果我搞砸了未来的任何事情,请告诉我。谢谢你的帮助!

3 个答案:

答案 0 :(得分: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 roll7。你也可以考虑尝试这个:

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

答案 1 :(得分: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)
... 
>>> 

答案 2 :(得分:1)

你的第一个问题是这一行:

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

如果roll不等于7,这将是一个无限循环。你应该在这里有一个and,因为如果一个,你想要改变如果两者。有意义吗?

你想:

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) 

double等于检查roll2是否等于左边的表达式。你想要做的是将它设置为左边的表达式,如下所示:

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