如果重复出现异常,则更改用户输入语句

Changing the user input statement if repeated exceptions

我正在尝试在python中编写一个吊人游戏 3.我选择一个玩家输入密码并检查以确保没有数字,特殊字符等。我是什么如果 other 玩家试图打破这些规则,希望能够让 other 玩家有机会选择单词。然后我希望它保持交替,直到有人输入符合条件的单词。我现在拥有的...

word_to_guess = input(First_player + " has been randomly chosen to pick a word! Please type it in now with no numbers, spaces or special characters: ").lower()
while True:
    if word_to_guess.isalpha():
        break
    word_to_guess = input("OK, " + Second_player + ", since " + First_player + " can't follow the rules, you try it. Again, no numbers, spaces or special characters: ").lower()

因此,如果第一个玩家获得它,我们继续,如果第一个玩家失败而第二个玩家获得它,我们继续,但如果他们都失败了,所编写的代码将继续为玩家 2 提供词的选择。我基本上只想在每次重复时交替最后一条语句中“First_player”和“Second_player”变量位置的位置。有什么想法吗?

我认为你可以使用标志变量,它会显示轮到谁了

word_to_guess = input(First_player + " has been randomly chosen to pick a word! Please type it in now with no numbers, spaces or special characters: ").lower()
is_first_player_turn = False
while True:
 if is_first_player_turn:
  #First player's turn
 else:
  #Second player's turn
 is_first_player_turn = not is_first_player_turn 

它认为第 5 行中硬编码的 First_player 和 Second_player 可能会让您有点失望。

word_to_guess = input(First_player + " has been randomly chosen to pick a word! Please type it in now with no numbers, spaces or special characters: ").lower()

turn_player = First_player
last_player = Second_player

while True:
    if word_to_guess.isalpha():
        break
    turn_player, last_player = last_player, turn_player
    word_to_guess = input("OK, " + turn_player + ", since " + last_player + " can't follow the rules, you try it. Again, no numbers, spaces or special characters: ").lower()