有没有办法在 python 中的两个值之间交替

Is there any way to alternate between two values in python

假设我有一个值 Player 1Player 2。我可以将它们分配给单个变量吗

print("%s choose this piece" %Player_value)

预期产出

Player 1 choose this value
Player 2 choose this value

这怎么行!

请帮忙!

>>> import itertools
>>> player = itertools.cycle(['Player 1', 'Player 2'])
>>> print("%s choose this piece" % next(player))
Player 1 choose this piece
>>> print("%s choose this piece" % next(player))
Player 2 choose this piece
>>> print("%s choose this piece" % next(player))
Player 1 choose this piece
...

有几种好方法可以进行这种交换。一种简单的方法是使用整数 1 和 2。交换将变为:

    Player_value = 3 - Player_value

另一种方法是有一个列表。这对两个人来说太过分了,但允许超过两个人。

    Players = [1, 2]
    print("Player %d choose:" % Players[0] )
    Players.append( Players.pop(0) )

创建一个字典,并遍历它。键是玩家名称,值是玩家值。

playerDict = {"Player1":"10","Player2":"20"}
for players in playerDict:print(f"{players} choose this {playerDict[players]}")

输出

Player1 choose this 10
Player2 choose this 20