而循环循环永远Python

时间:2014-04-15 21:04:20

标签: python loops while-loop forever

所以,我正在浏览网页上的craps程序模拟器,以及来自该论坛用户的一些帮助。我能够在大多数情况下完成它。至少,我认为我有正确的游戏逻辑。

上次我使用它时运行但是我必须在没有意识到的情况下改变了一些东西,因为现在while循环正在运行。在该计划中,骰子在没有用户干预的情况下永远滚动。

有人可以看一下,看看代码是否有问题?我已经在这个工作了几个小时,没有在哪里。

我再也不会赌博了! :/

from Dice import PairOfDice
print("Now Let's play with two dice!")
#
def MainDouble():
    bdice = PairOfDice()
    doubleDiceRoll = ''
    global myPoint #declare global variable
    input("Press Enter to Roll the Dice once")

    while doubleDiceRoll == '': #Roll the dice (both die objects)
        bdice.toss()
        print ('The first die reads.. ' + str(bdice.getValue1()) + '\n')
        print ('The second die reads.. ' + str(bdice.getValue2()) + '\n')

        Val = bdice.getTotal()

##Beginning of conditional blocks##
    if Val == 7 or Val == 11:
        gamestatus = "WON"


    elif Val == 2 or Val == 3 or Val == 12:
        gamestatus = "LOST"

    if Val != 7 and bdice.getTotal() != 11 and Val != 2 and Val != 3 and Val != 12:
        gamestatus = "CONTINUE"
            #
        myPoint = Val
        print("The Point is now " + myPoint + "/n") #display the user's point value
        global pSum 
        pSum = 0


    #The point

    while gamestatus == "CONTINUE": #Checking the point
            global point   
            point = myPoint   
            pSum = MainDouble()

            if pSum == myPoint:
             gamestatus == "WON"
            elif pSum == 7:
             gamestatus = "LOST"
            if gamestatus == "WON":
             print("Winner!")
            else:
             print("Sorry, Seven out!")
            print ("Roll Again?")

    doubleDiceRoll = input("Roll Again?")  

MainDouble()

2 个答案:

答案 0 :(得分:5)

这个区块:

while doubleDiceRoll == '': #Roll the dice (both die objects)
    bdice.toss()
    print ('The first die reads.. ' + str(bdice.getValue1()) + '\n')
    print ('The second die reads.. ' + str(bdice.getValue2()) + '\n')

    Val = bdice.getTotal()

doubleDiceRoll永远不会从''更改,因此此循环将永远运行。在这个块结束时(但仍在其中!),你应该做类似

的事情
    doubleDiceRoll = raw_input("Roll Again?") #Edit thanks to @Adam Smith

答案 1 :(得分:2)

我认为你在以下情况后搞砸了你的缩进:

##Beginning of conditional blocks##

这一行以下的所有内容都在while循环之外,但应该在其中。