Apr-24-2020, 02:13 AM
import random data = ["orange", "mango", "fridge", "mad", "smart", "love", "curtain", "chair", "table", "cable", "cake", "magic", "rapid", "talcum", "absent", "music", "radio", "house", "sofa", "kitchen", "napkin", "bucket", "awkward", "floor", "ceiling", "television", "handbag", "washbasin", "door", "window", "playground", "cricket", "basketball", "vehicle", "driver", "mirror", "bathroom", "photograph", "window"] print( """ Welcome to HANGMAN game Guess alphabets to find the right word you got same chances as count of alphabets Enter 'play' - to start game Enter 'quit' - to quit game """) player_wish = input("Enter your choice: ").lower() while player_wish == "play": Name = data.pop(random.randint(1, len(data) - 1)) word = [j for j in Name] word2 = word.copy() hangman = [] for i in range(len(word)): hangman.append("_") guess_count = len(word) - 1 attempt = [] while hangman != word or guess_count == 0: print("Guess this word") print(' '.join(hangman)) print("\n") print(f"You got {guess_count} chances left") alphabet = input("guess an alphabet: ").lower() attempt.append(alphabet) if alphabet in word: correct_index = word.index(alphabet) hangman.pop(correct_index) hangman.insert(correct_index, alphabet) while alphabet in word2: correct_index2 = word2.index(alphabet) word2.pop(correct_index2) word2.insert(correct_index2, "*") hangman.pop(correct_index2) hangman.insert(correct_index2, alphabet) print(f"\nYou got this one correct\nand \nyour attempted letters : {attempt}") else: print("\noops! wrong answer") print(f"You have {guess_count} chance left\n your attemped letters : {attempt}") if guess_count == 0: break guess_count -= 1 if guess_count != 0: print("you won!!\nYou guessed the word correctly") print(' '.join(hangman)) print("Want to play again?") player_desire = input("Enter 'yes' or 'no': ").lower() if player_desire == "yes": player_wish = "play" else: player_wish = "quit" print("Thanks for playing.") else: print(f"sorry, you lost the game\n The word was '{' '.join(word)}' ") print("Want to play again?") player_desire = input("Enter 'yes' or 'no': ").lower() if player_desire == "y": player_wish = "play" else: player_wish = "quit" print("Thanks for playing.")I have made hangman with little more interactive and can play over and over.
Professional Dentist(32years) fell in love with Python during COVID-19 Lockdown.
"Nothing can stop you from learning new things except your own will"
"Nothing can stop you from learning new things except your own will"