-2

我试图让 3 个 while 循环无限期地运行,但是现在当 while player == 2 循环结束时,由于函数,变量 player = 0,但循环不会回到第一个 while 循环。

def nextplayer():
    global player
    if player == 0:
        player = 1
    elif player ==1:
        player = 2
    elif player == 2:
        player = 0

while player == 0:
    print('Player 1 turn')
    spinwheel()
    nextplayer()

while player == 1:
    print('Player 2 turn')
    spinwheel()
    nextplayer()

while player == 2:
    print('Player 3 turn')
    spinwheel()
    nextplayer()
4

1 回答 1

0

这会简单得多itertools.cycle

from itertools import cycle


for player in cycle([0,1,2]):
    print(f'Player {player} turn')
    spinwheel()

无需调用nextplayer改变全局变量的函数。

于 2020-04-09T14:23:27.303 回答