我正在尝试用python 3编写一个hang-man游戏。我选择一名玩家输入密码,并进行检查,以确保没有数字、特殊字符等。如果当前玩家试图违反这些规则,我希望能够给其他球员选择密码的机会。然后我希望它一直交替,直到有人输入符合标准的单词。我现在拥有的。。。
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选择单词。我基本上只是想把";第一_;以及";第二层;最后一句话中的可变位置,每次重复。有什么想法吗?
它认为第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()
我认为您可以使用flag变量,它将显示轮到谁是
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