繁体   English   中英

While循环永远循环Python

[英]While loop looping forever Python

因此,我正在研究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()

该块:

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

我认为您在以下情况后弄乱了缩进:

##Beginning of conditional blocks##

该行下方的所有内容都在while循环之外,但可能应该在其中。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM