def game_loop(): # The line below shows the syntax for creating an instance of a class. We need the # parentheses after Hangman() here even though we're not passing in any parameters # because we need to tell Python that we want an *instance* of the class and not # the *class itself*. my_game = Hangman() print('#-=~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~=-#') print('Welcome to Python Hangman!') print('#-=~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~=-#') print('Your category is: %s' % my_game.puzzle['category']) # game_on determines whether or not we will play another game. It's initialized to 'Y' # because that's the value that means "play another game" and I'm assuming that my user # wants to play a game. You know, since they ran the program and everything. :) game_on = 'Y' while game_on.upper() == 'Y': while my_game.get_game_state() == 'in_progress': # As long as the game is in progress, we need to print out the game's state, # get a guess from our user, and then update the game wtih that state. print_game_state(my_game) guess = input('Guess (* to give up):') my_game.update_game(guess) # Game state needs to be presented to the user once more after the game completes # so that they can see the results of their game. It would be rude to keep it a # secret from them. print_game_state(my_game) print('#-=~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~=-#') # A new game is prepared in case our user wants to play again. my_game.start_random_puzzle() game_on = input('Play again? (y/n):') while game_on.upper() not in ['Y', 'N']: game_on = input( 'Input y or n. I\'m trying to be a cool program here and will let you input Y or N even though I didn\'t tell you those would work. But those are the options. YOU WILL CHOOSE ONE OF THEM. DON\'T YOU DARE PRESS CTRL+C TO QUIT THIS PROGRAM. I AM THE ONE WHO KNOCKS!' ) # It never hurts to be polite. print('Thanks for playing!')
else: validRes = True os.system("cls" if os.name == "nt" else "clear") if res.upper() == "Y": game = Hangman() game.play() winner = False strikes = 0 while not winner: try: print(game.get_game_state()) winner = game.check_guess(input("Guess a letter or an entire word: ")) strikes = game.get_strikes() except Exception as e: print(e) time.sleep(3) if strikes == 6: print(game.get_game_state()) print( f'You struck out. The word to guess was "{game.get_word_to_guess()}".' ) break if winner: