|
| 1 | +# Hangman in Python |
| 2 | +import random |
| 3 | + |
| 4 | +hangman_art = {0: (" ", |
| 5 | + " ", |
| 6 | + " "), |
| 7 | + 1: (" o ", |
| 8 | + " ", |
| 9 | + " "), |
| 10 | + 2: (" o ", |
| 11 | + " | ", |
| 12 | + " "), |
| 13 | + 3: (" o ", |
| 14 | + "/| ", |
| 15 | + " "), |
| 16 | + 4: (" o ", |
| 17 | + "/|\\", |
| 18 | + " "), |
| 19 | + 5: (" o ", |
| 20 | + "/|\\", |
| 21 | + "/ "), |
| 22 | + 6: (" o ", |
| 23 | + "/|\\", |
| 24 | + "/ \\")} |
| 25 | + |
| 26 | +words = ("aardvark", "alligator", "alpaca", "ant", "anteater", |
| 27 | + "antelope", "ape", "armadillo", "baboon", "badger", "bat", |
| 28 | + "bear", "beaver", "bee", "bison", "boar", "buffalo", |
| 29 | + "butterfly", "camel", "capybara", "caribou", "cat", |
| 30 | + "caterpillar", "cattle", "chamois", "cheetah", "chicken", |
| 31 | + "chimpanzee", "chinchilla", "chough", "clam", "cobra", "cockroach", |
| 32 | + "cod", "coyote", "crab", "crane", "crocodile", "crow", "curlew", "deer", |
| 33 | + "dinosaur", "dog", "dogfish", "dolphin", "donkey", "dormouse", "dotterel", "dove", |
| 34 | + "elephant", "elk", "emu", "falcon", "ferret", "finch", "fish", "flamingo", |
| 35 | + "fly", "fox", "frog", "gaur", "gazelle", "gerbil", "giraffe", "gnat", "gnu", |
| 36 | + "goat", "goldfinch", "goldfish", "goose", "gorilla", "goshawk", "grasshopper", |
| 37 | + "grouse", "guanaco", "gull", "hamster", "hare", "hawk", "hedgehog", "heron", "herring", "hippopotamus", |
| 38 | + "hornet", "horse", "human", "hummingbird", "hyena", "ibex", "ibis", "jackal", "jaguar", "jay", |
| 39 | + "jellyfish", "kangaroo", "kingfisher", "koala", "kookabura", "kouprey", "kudu", "lapwing", "lark", |
| 40 | + "lemur", "leopard", "lion", "llama", "lobster", "locust", "loris", "louse", "lyrebird", "magpie", "mallard", |
| 41 | + "manatee", "mandrill", "mantis", "marten", "meerkat", "mink", "mole", "mongoose", "monkey", "moose", "mosquito", |
| 42 | + "mouse", "mule", "narwhal", "newt", "nightingale", "octopus", "okapi", "opossum", "oryx", "ostrich", "otter", |
| 43 | + "owl", "ox", "oyster", "panda", "panther", "parrot", "partridge", "peafowl", "pelican", "penguin", "pheasant", |
| 44 | + "pig", "pigeon", "polar-bear", "pony", "porcupine", "porpoise", "quail", "quelea", "quetzal", "rabbit", "raccoon", |
| 45 | + "rail", "ram", "rat", "raven", "red-deer", "red-panda", "reindeer", "rhinoceros", "rook", "salamander", "salmon", |
| 46 | + "sand-dollar", "sandpiper", "sardine", "scorpion", "seahorse", "seal", "shark", "sheep", "shrew", "skunk", "snail", |
| 47 | + "snake", "sparrow", "spider", "spoonbill", "squid", "squirrel", "starling", "stingray", "stoat", "stork", "swallow", "swan", |
| 48 | + "tapir", "tarsier", "termite", "tiger", "toad", "trout", "turkey", "turtle", "viper", "vulture", "wallaby", "walrus", "wasp", |
| 49 | + "weasel", "whale", "wildcat", "wolf", "wolverine", "wombat", "woodcock", "woodpecker", "worm", "wren", "yak", "zebra") |
| 50 | + |
| 51 | +def display_man(wrong_guesses): |
| 52 | + print("**********") |
| 53 | + for line in hangman_art[wrong_guesses]: |
| 54 | + print(line) |
| 55 | + print("**********") |
| 56 | + |
| 57 | +def display_hint(hint): |
| 58 | + print(" ".join(hint)) |
| 59 | + |
| 60 | +def display_answer(answer): |
| 61 | + print(" ".join(answer)) |
| 62 | + |
| 63 | +def main(): |
| 64 | + answer = random.choice(words) |
| 65 | + hint = ["_"] * len(answer) |
| 66 | + wrong_guesses = 0 |
| 67 | + guessed_letters = set() |
| 68 | + is_running = True |
| 69 | + |
| 70 | + while is_running: |
| 71 | + display_man(wrong_guesses) |
| 72 | + display_hint(hint) |
| 73 | + guess = input("Enter a letter: ").lower() |
| 74 | + |
| 75 | + if len(guess) != 1 or not guess.isalpha(): |
| 76 | + print("Invalid input") |
| 77 | + continue |
| 78 | + |
| 79 | + if guess in guessed_letters: |
| 80 | + print(f"{guess} is already guessed") |
| 81 | + continue |
| 82 | + |
| 83 | + guessed_letters.add(guess) |
| 84 | + |
| 85 | + if guess in answer: |
| 86 | + for i in range(len(answer)): |
| 87 | + if answer[i] == guess: |
| 88 | + hint[i] = guess |
| 89 | + else: |
| 90 | + wrong_guesses += 1 |
| 91 | + |
| 92 | + if "_" not in hint: |
| 93 | + display_man(wrong_guesses) |
| 94 | + display_answer(answer) |
| 95 | + print("YOU WIN! THERE IS NOONE BETTER THAN YOU KING") |
| 96 | + is_running = False |
| 97 | + elif wrong_guesses >= len(hangman_art) - 1: |
| 98 | + display_man(wrong_guesses) |
| 99 | + display_answer(answer) |
| 100 | + print("YOU LOSE! BUT YOU SHOULD TRY AGAIN!!!!") |
| 101 | + is_running = False |
| 102 | + |
| 103 | +if __name__ == "__main__": |
| 104 | + main() |
0 commit comments