迭代器卡住,导致无限循环

时间:2013-03-27 18:13:59

标签: python for-loop python-2.7 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.'

导致此问题的原因是什么,以及如何修复?

2 个答案:

答案 0 :(得分:3)

您在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.

答案 1 :(得分:0)

您的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