我正在尝试制作“井字游戏”游戏,当前我的输出选择一个玩家并要求他为他选择一个牌子,它会显示所选玩家的姓名,此下一个玩家必须选择一个为他签名,我需要代码的帮助,该代码会询问未选择签名的其他玩家

import random

whose_turn = random.choice(['Player 1', 'Player 2'])
print(f"First Turn: {whose_turn}")
sign_player_1 = input(f"{whose_turn}, Choose a sign for you: ")
sign_player_2 = input(f"Opponent, Choose a sign for you: ")

Actual Output:

First Turn: Player 1
Player 1, Choose a sign for you: X
Opponent, Choose a sign for you: O

Expected Output:
First Turn: {Player chosen}
{Player chosen}, Choose a sign for you:
{Player not chosen}, Choose a sign for you:

最佳答案

您可以使用random.shuffle

from random import shuffle

players = ['Player 1', 'Player 2']
shuffle(players)
player1, player2 = players

print(f"First Turn: {player1}")
sign_player_1 = input(f"{player1}, Choose a sign for you: ")
sign_player_2 = input(f"{player2}, Choose a sign for you: ")

关于python - 我应该在代码中添加些什么,以便获得所需的输出,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/56334634/

10-12 22:50