From e87cbd59fae83a63c0b78e8e4cab28c266b8d43d Mon Sep 17 00:00:00 2001 From: santosh50 <47947735+santosh50@users.noreply.github.com> Date: Wed, 20 Oct 2021 18:26:43 +0530 Subject: [PATCH] Created Hangman game in Python --- Python/Python_Games/hangman.py | 64 ++++++++++++++++++++++++++++++++++ 1 file changed, 64 insertions(+) create mode 100644 Python/Python_Games/hangman.py diff --git a/Python/Python_Games/hangman.py b/Python/Python_Games/hangman.py new file mode 100644 index 0000000..0b6312a --- /dev/null +++ b/Python/Python_Games/hangman.py @@ -0,0 +1,64 @@ + +#Hangman game + +import random + +# Initialization +fruit_list = [ + 'apple', + 'banana', + 'orange', + 'mango', + 'strawberry', + 'watermelon', + 'kiwi', + 'peach', + 'pear', + 'papaya', + 'grapes', + 'pineapple', + 'guava', + 'blueberry', + 'blackberry', + 'raspberry', + 'apricot'] + +choice = 'Y' + +# Game Start +while choice == 'Y': + #Setup + fruit = random.choice(fruit_list) + lives = 5 + temp = [] + word = [] + + for i in fruit: + word.append(i) + temp.append('_') + + print("\n\tHANGMAN!") + + #Gameplay + while '_' in temp and lives!=0: + print() #newline + for i in temp: + print(i, end = ' ') + print("\t Lives:", lives, '\n') + l = input("Guess a letter: ").lower() + + if l in word: + for i in range(0,len(word)): + if word[i] == l: + temp[i] = l + else: + lives -= 1 + + # Game End + if lives == 0: + print("\nGAME OVER!") + else: + print("\nYOU WIN!!!") + print("The word was", fruit.upper()) + + choice = input("\nPlay Again?(y/n) ").upper() \ No newline at end of file