Reputation: 63
I want to add a play again feature to this hangman code, which will ask you to play again after you've either won or lost the match. Here's the incomplete play again code I have currently:
playagain = input('Play again? (Y/N) ').upper()
if playagain == 'Y':
word = random.choice(wordbank)
I have no idea where to start.
Here's my complete game code:
# 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/ \ '
]
# basic functions of the game
mistakes = 0
letters_guessed = []
mistakes_allowed = len(hangman_graphics)
word = random.choice(wordbank) # selecting a random word for the user to guess
# letters user has guessed + guessed incorrectly stored in lists
letters_word = list(word)
wrong_letters = []
print()
# amount of letters the word has
print('The word has {} letters'.format(len(letters_word)))
# 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('{}, '.format(letter), end='')
print()
print('Guesses left: {}'.format(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, 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
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('Play again? (Y/N) ').upper()
if playagain == 'Y':
word = random.choice(wordbank)
# ending: user loses
if mistakes == mistakes_allowed:
print()
print('Unlucky, better luck next time!')
print()
print(f'The word was {word}.')
print()
playagain = input('Play again? (Y/N) ').upper()
if playagain == 'Y':
word = random.choice(wordbank)
Upvotes: 0
Views: 314
Reputation: 492
Wrap your code inside a While
loop and check on whether he wants to play again whether he wins or loses.
While playagain == 'Y':
...
add a break after he wins
# 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/ \ '
]
playagain = 'Y'
while playagain == 'Y':
# basic functions of the game
mistakes = 0
letters_guessed = []
mistakes_allowed = len(hangman_graphics)
word = random.choice(wordbank) # selecting a random word for the user to guess
# letters user has guessed + guessed incorrectly stored in lists
letters_word = list(word)
wrong_letters = []
print()
# amount of letters the word has
print('The word has {} letters'.format(len(letters_word)))
# 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('{}, '.format(letter), end='')
print()
print('Guesses left: {}'.format(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, 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
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('Play again? (Y/N) ').upper()
if playagain == 'Y':
word = random.choice(wordbank)
break
# ending: user loses
if mistakes == mistakes_allowed:
print()
print('Unlucky, better luck next time!')
print()
print(f'The word was {word}.')
print()
playagain = input('Play again? (Y/N) ').upper()
if playagain == 'Y':
word = random.choice(wordbank)
Upvotes: 1
Reputation: 220
You can declare a variable before all of the game logic occurs named for example playagain which will recevie a boolean value (at first true)
playagain = True
, and put all of the game logic inside a while loop that checks if playagain equals to true.
while (playagain):
# Game Logic
# Then at the end of the game logic but still inside the while
# loop ask the user if he wants to continue to play
keepPlaying = input('Play again? (Y/N) ').upper()
if keepPlaying != 'Y':
playagain = False
Upvotes: 1