From 61bd3dc4b75641f46e247cff98714f5295d3e59a Mon Sep 17 00:00:00 2001 From: NIKITA PANDEY <113332472+nikitapandeyy@users.noreply.github.com> Date: Sat, 18 Mar 2023 19:13:05 +0530 Subject: [PATCH] Create hangman_game.py creating hangman_game python file . --- hangman_game.py | 46 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 46 insertions(+) create mode 100644 hangman_game.py diff --git a/hangman_game.py b/hangman_game.py new file mode 100644 index 00000000..c67677e7 --- /dev/null +++ b/hangman_game.py @@ -0,0 +1,46 @@ +import random + +# list of words to choose from +words = ['apple', 'banana', 'cherry', 'orange', 'pear'] + +# function to choose a random word +def choose_word(words): + return random.choice(words) + +# function to play the game +def play_game(word): + guesses = '' + turns = 10 + while turns > 0: + incorrect = 0 + for letter in word: + if letter in guesses: + print(letter, end=' ') + else: + print('_', end=' ') + incorrect += 1 + if incorrect == 0: + print('\nYou win!') + break + guess = input('\nGuess a letter: ') + guesses += guess + if guess not in word: + turns -= 1 + print('Incorrect! You have', turns, 'turns left.') + if turns == 0: + print('Game over. The word was', word) + +# main function to run the game +def main(): + print('Welcome to Hangman!') + word = choose_word(words) + play_game(word) + play_again = input('Do you want to play again? (y/n) ') + if play_again.lower() == 'y': + main() + else: + print('Thanks for playing!') + +# run the game +if __name__ == '__main__': + main()