Jul-07-2020, 01:19 PM
To test the play again logic try entering something other than Yes or No. I guess I did a typo (probably just Y or N) when I tested. Your logic has three possible outcomes; quit, replay with a new word; replay with the old word already solved.
Your replay logic is in the wrong place and that is why you have to use sys.exit to quit the program. The main body of the program should manage the replay logic and the run function should implement the game logic. By removing the replay logic from the run function you can initialize the game at the top of the run function, removing the initialization stuff from the random_word function (or removing the random word function).
Your replay logic is in the wrong place and that is why you have to use sys.exit to quit the program. The main body of the program should manage the replay logic and the run function should implement the game logic. By removing the replay logic from the run function you can initialize the game at the top of the run function, removing the initialization stuff from the random_word function (or removing the random word function).
"""Hangman game""" import random def play_game(): """Player enters guesses until all letters in word are guessed""" # Initialize word, guesses, etc while '_' in list_space1: # get guess # process guess print('You guessed the word!') while True: play_game() if input('Would you like to play again (y/n)? ').upper()[0] == 'N': print('Thanks for playing!') break;If you don't want to reorganize the replay logic you can still get rid of the sys.exit() call. In run:
if ask.upper()[0] == 'Y': random_word() else: run_game = False returnStill so ugly.