python - 重玩功能仅在用户输掉游戏时才起作用(刽子手)
问题描述
因此,在用户赢或输后,他们会被询问是否要再次玩(Y/N)。
这里应该发生 3 件事:
他们输入 Y 并开始新游戏。
他们键入 N,游戏结束。
他们输入不同的字符,在这种情况下他们会收到消息:请输入有效的答案。(Y/N),他们被问到是否想再玩一次
当用户输了并且他们被要求再次玩时,上述所有三个标准都有效。但是,当用户获胜并被要求再次玩时,只有无效字符消息有效。当他们输入 Y 时,他们会进入一个新游戏,除了你已经猜到的字母不会重置 + 一次猜测后,单词会立即显示并且他们再次“获胜”(你“猜猜”也不要“ t 重置)。当他们输入 N 时,游戏继续。
以下是问题的图片: https ://imgur.com/gallery/PWiSTWP
我完全不知道发生了什么,因为我为每个场景使用了相同的代码(用户获胜或用户失败):
playagain = input('Wanna play again? (Y/N) ').upper()
if playagain == 'Y':
word = random.choice(wordbank)
elif playagain == 'N':
print('Alright, goodbye.')
while playagain != 'Y' and playagain != 'N':
print('Please type a valid answer. (Y/N)')
print()
playagain = input('Wanna play again? (Y/N) ').upper()
这是我的整个游戏代码:
# 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' and playagain != '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' and playagain != 'N':
print('Please type a valid answer. (Y/N)')
print()
playagain = input('Wanna play again? (Y/N) ').upper()
解决方案
检查玩家获胜的块位于具有条件的 while 循环内mistakes < mistakes_allowed
。由于该条件仍然成立并且没有break
语句,它只是继续从那里重复,而不是退回到足够远以重复整个playagain == 'Y'
循环。您需要更改该循环条件或添加break
语句。
推荐阅读
- android - sdk manager visual studio中的错误“找不到中央目录”
- assembly - 如何用null修改字符串中的字符
- mysql - 在 mysql docker 容器中禁用 ONLY_FULL_GROUP_BY 模式
- android - 如果我们已经在 app/build.gradle 中设置了 AndroidManifest.xml 中的 versionCode 和 versionName,需要什么?
- python - 如何将存储为txt的列表导入熊猫?
- maven - 在 Red Hat Enterprise Linux 8.3 版上安装 Maven:未显示 Maven 版本
- java - 通过语言识别将项目从基于火的数据库卸载到回收卡
- datagrid - 如何使我的布尔列在 ag-grid 中显示复选框
- android - 在 android 中使用 OPENGLES 绘制天空网格(球体)
- javascript - 重定向到条带结帐时出错