。我写了一个for循环,循环遍历玩家,为每个人掷骰子,然后更新他们的位置。。这是我的代码:

player1location = 0

def turn(numberPlayers, player, player1location, Board):
    for player in range(numberPlayers):
        player = 'Player'+str(player+1)
        print 'It\'s', player, 'turn!'
        print player1location
        rollDice = raw_input('Press Enter to roll the dice!')
        diceRoll = random.randint(1,6)
        print player, 'rolled a', diceRoll
        player1location = player1location + diceRoll
        print 'You landed on', player1location
        print '\n'

while True:
    turn(numberPlayers, player, player1location, Board)

如果需要的话,我可以提供更多的代码,但我认为这是控制玩家位置的一切。谢谢!
。?

最佳答案

您的函数参数与要更新的目标变量同名。
。这是因为function为传递给函数的参数创建了一个本地范围。。

关于python - Python-在For循环中更新变量,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/14858364/

10-11 17:44