虽然循环不会在 python 中停止 [英] While loop won't stop in python

查看:50
本文介绍了虽然循环不会在 python 中停止的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

We are trying to make a python program to simulate games of craps and display the probability of winning. I have narrowed down the answer to the while loop and as the title suggests, when it hits the loop it won't exit. As far as I can tell the loop should exit but instead just returns and endless train of "false". Here is the code for the whole program with comments, I'll type out the rules for craps (as far as what we're using for our program) at the bottom.

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

The basic rules we are using are: the player rolls 2 six sided dice, if the initial roll is a 7 or 11 the player wins (line 4). If the player rolls a 2, 3 or a 12 the player loses (line 6) If the player doesn't get either they keep rolling until they match the initial roll or roll a 7. If they match the initial roll they win, if they get a 7 they lose (lines 10-15). Our program is supposed to simulate and display the probability of the player winning.

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

This is my first time using this site so let me know if I screwed up on anything for the future. And thanks for helping!

推荐答案

一个常见但非常令人沮丧且浪费时间的错别字,即使是最优秀的 Python 开发人员也会遇到.将 roll2 == random.randint(1,6) + random.randint(1,6) 替换为 roll2 = random.randint(1,6) + random.randint(1,6)).

A common, but very frustrating and time-wasting typo, which can get even the best Python developers. Replace roll2 == random.randint(1,6) + random.randint(1,6) with roll2 = random.randint(1,6) + random.randint(1,6).

此外,我认为您需要 roll2 != roll 和 roll2 != 7.从文体的角度来看,最好省略在 ifwhile 行中计算的语句周围的括号.

Also, I think you need roll2 != roll and roll2 != 7. From a stylistic point of view, it's better to omit the parenthesis around the statement evaluated in an if or while line.

不过,您的循环中有少量冗余.您在每个循环的顶部和每个循环的末尾检查 roll2roll7 .你也可以考虑试试这个:

Your loop has a slight amount of redundancy in it though. You check for roll2 being roll or 7 at the top of each loop and at the end of each loop. You could also consider trying this:

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

这篇关于虽然循环不会在 python 中停止的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆