簡體   English   中英

迭代器卡住,導致無限循環

[英]Iterator stuck, causing infinite loop

在Python 2.7.3中,我創建了一個2人游戲的自動游戲,其中2個玩家在輪到時分別來自以下角色:2、3、4、5、6。卡是6,回合結束。 否則,將卡的2個值相加,擲出2個骰子並將其值相加。 玩家該回合獲得的總點數是兩個紙牌值的總和乘以兩個骰子值的總和。 第一個獲得300分的玩家將贏得比賽。

我希望能夠執行100次“游戲”,每次顯示結果,並在所有游戲結束時顯示每個玩家獲勝的總次數。 但是,我在for循環中迭代的game變量停留在1 ,即第一個游戲,導致無限循環。

這是我的代碼:

import random
playerone = 0
playertwo = 0
playeronewins = 0
playertwowins = 0
currentplayer = 1
scoreToWin = 300
games = 100
for game in range(1,games+1):
    while playerone < scoreToWin and playertwo < scoreToWin:
        cardone = random.randint(2,6)
        cardtwo = random.randint(2,6)
        if cardone != 6 and cardtwo != 6:
            cardtotal = cardone + cardtwo
            dieone = random.randint(1,6)
            dietwo = random.randint(1,6)
            dietotal = dieone + dietwo
            points = cardtotal * dietotal
            if currentplayer == 1:
                playerone += points
            else:
                playertwo += points
            if playerone >= scoreToWin:
                print 'Game ' + str(game) + ': Player 1 wins with ' + str(playerone) + ' points. Player 2 loses with ' + str(playertwo) + ' points.'
                playeronewins += 1
                if game != games:
                    playerone = 0
                    playertwo = 0
            if playertwo >= scoreToWin:
                print 'Game ' + str(game) + ': Player 2 wins with ' + str(playertwo) + ' points. Player 1 loses with ' + str(playerone) + ' points.'
                playertwowins += 1
                if game != games:
                    playerone = 0
                    playertwo = 0
        currentplayer = 2 if currentplayer == 1 else 1
print 'Player 1 won ' + str(playeronewins) + ' times.'
print 'Player 2 won ' + str(playertwowins) + ' times.'

是什么導致此問題,如何解決?

您可以在while循環內將玩家分數設置為0,這樣它將永遠持續下去。

降低分數測試:

while playerone < scoreToWin and playertwo < scoreToWin:
    # card game loop
    # this line still belongs in the loop:
    currentplayer = 2 if currentplayer == 1 else 1

# *Now* test for the scores, *outside* the while loop
if playerone >= scoreToWin:
    print 'Game ' + str(game) + ': Player 1 wins with ' + str(playerone) + ' points. Player 2 loses with ' + str(playertwo) + ' points.'
    playeronewins += 1
if playertwo >= scoreToWin:
    print 'Game ' + str(game) + ': Player 2 wins with ' + str(playertwo) + ' points. Player 1 loses with ' + str(playerone) + ' points.'
    playertwowins += 1
playerone = 0
playertwo = 0

也無需測試game != games

通過這些更改,測試運行將產生:

Game 1: Player 2 wins with 330 points. Player 1 loses with 214 points.
Game 2: Player 2 wins with 301 points. Player 1 loses with 261 points.
Game 3: Player 1 wins with 348 points. Player 2 loses with 207 points.
# .. ~ ..
Game 98: Player 2 wins with 344 points. Player 1 loses with 248 points.
Game 99: Player 1 wins with 323 points. Player 2 loses with 173 points.
Game 100: Player 2 wins with 354 points. Player 1 loses with 105 points.
Player 1 won 45 times.
Player 2 won 55 times.

while循環永遠不會終止,因此您永遠也不會退出游戲1。最佳解決方案是重構代碼,以便於理解和推理。 (提示:使用某些功能。)但是,作為對原始代碼的快速修復,只需添加以下break語句:

        if playerone >= scoreToWin:
            print 'Game ' + str(game) + ': Player 1 wins with ' + str(playerone) + ' points. Player 2 loses with ' + str(playertwo) + ' points.'
            playeronewins += 1
            if game != games:
                playerone = 0
                playertwo = 0
   >>>>     break
        if playertwo >= scoreToWin:
            print 'Game ' + str(game) + ': Player 2 wins with ' + str(playertwo) + ' points. Player 1 loses with ' + str(playerone) + ' points.'
            playertwowins += 1
            if game != games:
                playerone = 0
                playertwo = 0
   >>>>     break

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM