再次播放功能导致在刽子手游戏中被要求再次播放的循环
Play again feature resulting in loop of getting asked to play again in hangman game
我有一个简单的重玩功能,在游戏结束时(在用户输赢之后)他们可以选择重玩。然而,如果用户没有键入 'Y'(是)或 'N'(否),那么他们应该得到消息 'Please type a valid answer.'。这是行不通的。实际发生的情况是,用户在输入任何字符(甚至是或否)后,都会陷入被要求再次玩游戏的循环中。
playagain = input('Wanna play again? (Y/N) ').upper()
if playagain == 'Y':
word = random.choice(wordbank)
elif playagain == 'N':
print('Alright, goodbye.')
while playagain != 'Y' or 'N':
print('Please type a valid answer. (Y/N)')
print()
playagain = input('Wanna play again? (Y/N) ').upper()
如果我删除“或 'N'”,则再次播放的选项有效,但前提是用户输入是。如果他们输入 no,那么他们就会遇到再次被要求玩的循环。
我该如何解决这个问题?
这是我的全部代码:
# importing wordbank
import random
from wordbankcool import wordbank
# hangman graphics
hangman_graphics = ['_',
'__',
'__\n |',
'__\n |\n O',
'__\n |\n O\n |',
'__\n |\n O\n/|',
'__\n |\n O\n/|\ ',
'__\n |\n O\n/|\ \n/',
'__\n |\n O\n/|\ \n/ \ '
]
# code is inside while loop
playagain = 'Y'
while playagain == 'Y':
# alphabet
alphabet = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l',
'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z']
# basic functions of the game
mistakes = 0
letters_guessed = []
mistakes_allowed = len(hangman_graphics)
# selecting a random word for the user to guess
word = random.choice(wordbank)
# letters user has guessed stored in list, making each letter of the word seperate
letters_word = list(word)
wrong_letters = []
print()
# amount of letters the word has
print(f'The word has {len(letters_word)} letters')
# while loop which will run until the the number of mistakes = number of mistakes allowed
while mistakes < mistakes_allowed:
print()
print('Incorrect guesses: ', end='')
for letter in wrong_letters:
print(f'{letter}, ', end='')
print()
print(f'Guesses left: {mistakes_allowed - mistakes}')
letter_user = input('Guess a letter: ').lower()
# checking if the letter has been guessed before
while letter_user in letters_guessed or letter_user in wrong_letters:
print()
print('You have already guessed this letter. Please guess a different one.')
letter_user = input('Guess a letter: ')
# increasing amount of mistakes if the letter that has been guessed is not in the word + checking if guess is a letter
if letter_user not in alphabet:
print('Please only enter A LETTER.')
continue
if letter_user not in letters_word:
mistakes += 1
wrong_letters.append(letter_user)
print()
# showing how many letters the user has/has not guessed
print('Word: ', end='')
# if letter is in word, its added to letters guessed
for letter in letters_word:
if letter_user == letter:
letters_guessed.append(letter_user)
# replace letters that haven't been guessed with an underscore
for letter in letters_word:
if letter in letters_guessed:
print(letter + ' ', end='')
else:
print('_ ', end='')
print()
# hangman graphics correlate with amount of mistakes made
if mistakes:
print(hangman_graphics[mistakes - 1])
print()
print('-------------------------------------------') # seperator
# ending: user wins
if len(letters_guessed) == len(letters_word):
print()
print(f'You won! The word was {word}!')
print()
playagain = input('Wanna play again? (Y/N) ').upper()
if playagain == 'Y':
word = random.choice(wordbank)
elif playagain == 'N':
print('Alright, goodbye.')
while playagain != 'Y' or 'N':
print('Please type a valid answer. (Y/N)')
print()
playagain = input('Wanna play again? (Y/N) ').upper()
# ending: user loses
if mistakes == mistakes_allowed:
print()
print('Unlucky, better luck next time.')
print()
print(f'The word was {word}.')
print()
playagain = input('Wanna play again? (Y/N) ').upper()
if playagain == 'Y':
word = random.choice(wordbank)
elif playagain == 'N':
print('Alright, goodbye.')
while playagain != 'Y' or 'N':
print('Please type a valid answer. (Y/N)')
print()
playagain = input('Wanna play again? (Y/N) ').upper()
您目前的情况:
while playagain != 'Y' or 'N':
or
运算符需要两个布尔值。所以 Python 将尝试将 'N'
转换为布尔值。由于 'N'
是一个非空字符串,它的计算结果将是 True
。因此,您所拥有的条件将等同于:
while playagain != 'Y' or True:
这将始终计算为 True。
要解决此问题,while 条件应如下所示。请注意,我们还将 or
替换为 and
,以使逻辑正常。
while playagain != 'Y' and playagain != 'N':
我们必须为两者写下完整的条件。
尝试这样做:
while playagain!='Y' and playagain!='N':
我有一个简单的重玩功能,在游戏结束时(在用户输赢之后)他们可以选择重玩。然而,如果用户没有键入 'Y'(是)或 'N'(否),那么他们应该得到消息 'Please type a valid answer.'。这是行不通的。实际发生的情况是,用户在输入任何字符(甚至是或否)后,都会陷入被要求再次玩游戏的循环中。
playagain = input('Wanna play again? (Y/N) ').upper()
if playagain == 'Y':
word = random.choice(wordbank)
elif playagain == 'N':
print('Alright, goodbye.')
while playagain != 'Y' or 'N':
print('Please type a valid answer. (Y/N)')
print()
playagain = input('Wanna play again? (Y/N) ').upper()
如果我删除“或 'N'”,则再次播放的选项有效,但前提是用户输入是。如果他们输入 no,那么他们就会遇到再次被要求玩的循环。 我该如何解决这个问题?
这是我的全部代码:
# importing wordbank
import random
from wordbankcool import wordbank
# hangman graphics
hangman_graphics = ['_',
'__',
'__\n |',
'__\n |\n O',
'__\n |\n O\n |',
'__\n |\n O\n/|',
'__\n |\n O\n/|\ ',
'__\n |\n O\n/|\ \n/',
'__\n |\n O\n/|\ \n/ \ '
]
# code is inside while loop
playagain = 'Y'
while playagain == 'Y':
# alphabet
alphabet = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l',
'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z']
# basic functions of the game
mistakes = 0
letters_guessed = []
mistakes_allowed = len(hangman_graphics)
# selecting a random word for the user to guess
word = random.choice(wordbank)
# letters user has guessed stored in list, making each letter of the word seperate
letters_word = list(word)
wrong_letters = []
print()
# amount of letters the word has
print(f'The word has {len(letters_word)} letters')
# while loop which will run until the the number of mistakes = number of mistakes allowed
while mistakes < mistakes_allowed:
print()
print('Incorrect guesses: ', end='')
for letter in wrong_letters:
print(f'{letter}, ', end='')
print()
print(f'Guesses left: {mistakes_allowed - mistakes}')
letter_user = input('Guess a letter: ').lower()
# checking if the letter has been guessed before
while letter_user in letters_guessed or letter_user in wrong_letters:
print()
print('You have already guessed this letter. Please guess a different one.')
letter_user = input('Guess a letter: ')
# increasing amount of mistakes if the letter that has been guessed is not in the word + checking if guess is a letter
if letter_user not in alphabet:
print('Please only enter A LETTER.')
continue
if letter_user not in letters_word:
mistakes += 1
wrong_letters.append(letter_user)
print()
# showing how many letters the user has/has not guessed
print('Word: ', end='')
# if letter is in word, its added to letters guessed
for letter in letters_word:
if letter_user == letter:
letters_guessed.append(letter_user)
# replace letters that haven't been guessed with an underscore
for letter in letters_word:
if letter in letters_guessed:
print(letter + ' ', end='')
else:
print('_ ', end='')
print()
# hangman graphics correlate with amount of mistakes made
if mistakes:
print(hangman_graphics[mistakes - 1])
print()
print('-------------------------------------------') # seperator
# ending: user wins
if len(letters_guessed) == len(letters_word):
print()
print(f'You won! The word was {word}!')
print()
playagain = input('Wanna play again? (Y/N) ').upper()
if playagain == 'Y':
word = random.choice(wordbank)
elif playagain == 'N':
print('Alright, goodbye.')
while playagain != 'Y' or 'N':
print('Please type a valid answer. (Y/N)')
print()
playagain = input('Wanna play again? (Y/N) ').upper()
# ending: user loses
if mistakes == mistakes_allowed:
print()
print('Unlucky, better luck next time.')
print()
print(f'The word was {word}.')
print()
playagain = input('Wanna play again? (Y/N) ').upper()
if playagain == 'Y':
word = random.choice(wordbank)
elif playagain == 'N':
print('Alright, goodbye.')
while playagain != 'Y' or 'N':
print('Please type a valid answer. (Y/N)')
print()
playagain = input('Wanna play again? (Y/N) ').upper()
您目前的情况:
while playagain != 'Y' or 'N':
or
运算符需要两个布尔值。所以 Python 将尝试将 'N'
转换为布尔值。由于 'N'
是一个非空字符串,它的计算结果将是 True
。因此,您所拥有的条件将等同于:
while playagain != 'Y' or True:
这将始终计算为 True。
要解决此问题,while 条件应如下所示。请注意,我们还将 or
替换为 and
,以使逻辑正常。
while playagain != 'Y' and playagain != 'N':
我们必须为两者写下完整的条件。 尝试这样做:
while playagain!='Y' and playagain!='N':