我应该在代码中添加些什么,以便获得所需的输出

时间:2019-05-28 03:32:14

标签: python python-3.x

我正在尝试制作“井字游戏”游戏,目前我的输出选择了一个玩家,并要求他为他选择一个标志,它会显示该玩家的姓名,此下一个玩家拥有为他选择一个标志,我需要帮助代码,该代码将询问未选择标志的其他玩家

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:

1 个答案:

答案 0 :(得分:1)

您可以使用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: ")