# Hangman in python import random # Easy words (common objects, foods, simple nouns) easy_words = [ "apple", "banana", "orange", "grape", "peach", "water", "light", "house", "chair", "table", "music", "plant", "sunny", "bread", "cloud", "candy", "pizza", "shoes", "pencil", "phone" ] # Medium words (cities, animals, common movies/TV characters) medium_words = [ "london", "paris", "tiger", "eagle", "rabbit", "frozen", "shrek", "joker", "penguin", "canada", "guitar", "camera", "window", "castle", "pirate", "dancer", "rocket", "cookie", "monkey", "garden" ] # Hard words (tricky spelling, uncommon letters, abstract concepts) hard_words = [ "awkward", "rhythm", "zephyr", "mystify", "jigsaw", "cryptic", "oxygen", "numbskull", "buzzing", "voodoo", "blizzard", "zodiac", "subway", "knapsack", "quizzes", "jukebox", "vortex", "pixelate", "sphinx", "jackpot" ] # Combine all words (optional) words = easy_words + medium_words + hard_words #Dictionary of key:() hangman_art = {0: (" ", " ", " "), 1: (" o ", " ", " "), 2: (" o ", " | ", " "), 3: (" o ", "/| ", " "), 4: (" o ", "/|\\", " "), 5: (" o ", "/|\\", "/ "), 6: (" o ", "/|\\", "/ \\")} def string_checker(question, valid_ans=("hard", "medium", "easy")): error = f"Please enter a valid option from the following list: {valid_ans}" while True: # Get user response and make sure it's lowercase user_response = input(question).lower() for item in valid_ans: # check if the user response is a word in the list if item == user_response: return item # check if the user response is the same as # the first letter of an item in the list elif user_response == item[0]: return item # print error if user does not enter something that is valid print(error) print() def difficulty_checker(question, valid_ans=("yes", "no")): error = f"Please enter a valid option from the following list: {valid_ans}" while True: # Get user response and make sure it's lowercase user_response = input(question).lower() for item in valid_ans: # check if the user response is a word in the list if item == user_response: return item # check if the user response is the same as # the first letter of an item in the list elif user_response == item[0]: return item # print error if user does not enter something that is valid print(error) print() # Displays instructions def instruction(): print(''' **** Instructions **** To begin, choose the number of rounds (or press for infinite mode). Then play against the computer. You need to choose R (rock), P (paper) or S (scissors). The rules are as follows: o Paper beats rock o Rock beats scissors o Scissors beats paper Press to end the game at anytime. Good Luck! ''') want_instructions = string_checker("Do you want to read the instructions? ") # checks users enter yes (y) or no (n) if want_instructions == "yes": instruction() def game_difficulty(): game_difficulty = difficulty_checker("Would you like hard, medium or easy difficulty? ") def display_man(wrong_guesses): print("***********") for line in hangman_art[wrong_guesses]: print(line) print("***********") def display_hint(hint): print(" ".join(hint)) def display_answer(answer): print(" ".join(answer)) def main(): answer = random.choice(words) hint= ["_"] * len(answer) wrong_guesses = 0 guessed_letters = set() is_running = True while is_running: display_man(wrong_guesses) display_hint(hint) guess = input("Enter a letter: ").lower() # If user choice is the exit code, break the loop if guess == "stop": break if guess == answer: print("YOU WIN") is_running = False break if len(guess) !=1 or not guess.isalpha(): print("Invalid input") continue if guess in guessed_letters: print (f"{guess} is already guessed") continue guessed_letters.add(guess) if guess in answer: for i in range(len(answer)): if answer[i] == guess: hint[i] = guess else: wrong_guesses += 1 if "_" not in hint: display_man(wrong_guesses) display_answer(answer) print ("YOU WIN") is_running= False elif wrong_guesses >= len(hangman_art) - 1: display_man(wrong_guesses) print("YOU LOSE") is_running= False main()