【问题标题】:While loop won't stop in python虽然循环不会在python中停止
【发布时间】:2014-06-06 05:25:12
【问题描述】:

我们正在尝试制作一个 python 程序来模拟掷骰子游戏并显示获胜的概率。我已经缩小了while循环的答案,正如标题所示,当它到达循环时它不会退出。据我所知,循环应该退出,而只是返回和无休止的“假”列车。这是带有 cmets 的整个程序的代码,我将在底部输入掷骰子的规则(就我们在程序中使用的而言)。

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 行)。我们的程序应该模拟并显示玩家获胜的概率。

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

【问题讨论】:

  • 大概 def 语句(需要冒号)延伸到 win=0 行?

标签: python random while-loop


【解决方案1】:

一个常见但非常令人沮丧和浪费时间的拼写错误,即使是最优秀的 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

【讨论】:

  • 哦该死的...我发誓我以为我已经检查了上下语法错误。谢谢!
  • 这实际上是一个逻辑错误,因为它可以编译但不能按预期工作。并且在and 上很好地抓住了@SimonT,我以为我是唯一一个看到这个的人。
【解决方案2】:

双等号测试是否相等。将其更改为 single 以使您的代码正常工作。这是您编辑的代码:

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)
... 
>>> 

【讨论】:

  • 也谢谢您!让这样一个简单的错误占了上风,我感到很尴尬。我一直无法记住使用双等号来检查是否相等。现在我不小心用它来分配。
  • :) 没问题,记得在15分钟后接受对你帮助最大的答案,谢谢!
【解决方案3】:

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

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

如果 roll 不等于 7,这将是一个无限循环。您应该在此处使用 and,因为如果其中 onefalsenot 如果两者都是。有意义吗?

你想要:

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 equals 检查roll2 是否等于左边的表达式。您要做的是将其设置为等于左侧的表达式,如下所示:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-03-16
    • 2013-03-25
    • 1970-01-01
    • 2020-12-11
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多