So after the user either wins or loses, they get asked if they want to play again (Y/N).
Here 3 things that are supposed to happen:
They type Y and they start a new game.
They type N and the game closes.
They type a different character, in which case they get the message: Please type a valid answer. (Y/N), and they get asked if they want to play again
When the user loses and they get asked to play again, all three of the criteria above work. However, when the user wins and is asked to play again, only the invalid character message works. When they type Y, they get put in a new game except the letters you have already guessed don't reset after one guess, the word is revealed instantly and they 'win' again (you're 'guesses left' also don't reset). When they type N, then the game continues.
Here are images of the problems: https://imgur.com/gallery/PWiSTWP
I have absolutely no clue of whats going on because I used the same code for each scenario (user wins or user loses):
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()
Here's my whole gamecode:
# 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()
CodePudding user response:
The block that checks for the player winning is inside the while loop that has the condition mistakes < mistakes_allowed
. Since that condition remains true and there is no break
statement, it just continues to repeat from there rather than getting back out far enough to repeat the whole playagain == 'Y'
loop. You either need to change that loop condition or add a break
statement.