Skip to content

Instantly share code, notes, and snippets.

@Natgho
Created November 18, 2019 21:11
Show Gist options
  • Save Natgho/373c2dc5b14040516da8f5178827e5b4 to your computer and use it in GitHub Desktop.
Save Natgho/373c2dc5b14040516da8f5178827e5b4 to your computer and use it in GitHub Desktop.
hangman
import random
from time import sleep
import os
def clear_screen():
if os.name == 'nt':
os.system("cls")
else:
os.system("clear")
HANGMAN = (
"""
""",
"""
----------
""",
"""
|
|
|
|
|
|
|
|
----------
""",
"""
------
|
|
|
|
|
|
|
|
----------
""",
"""
------
| |
|
|
|
|
|
|
|
----------
""",
"""
------
| |
| O
|
|
|
|
|
|
----------
""",
"""
------
| |
| O
| -+-
| |
| |
|
|
|
----------
""",
"""
------
| |
| O
| /-+-
| |
| |
|
|
|
----------
""",
"""
------
| |
| O
| /-+-/
| |
| |
|
|
|
----------
""",
"""
------
| |
| O
| /-+-/
| |
| |
| |
| |
|
----------
""",
"""
------
| |
| O
| /-+-/
| |
| |
| | |
| | |
|
----------
""")
WORDS = (
"JANUARY", "FEBRUARY", "MARCH", "APRIL", "MAY", "JUNE", "JULY", "AUGUST", "SEPTEM", "OCTOBER", "NOVEMBER",
"DECEMBER",
"FALL", "WINTER", "SPRING", "SUMMER", "ELEPHANT", "BEAR", "FOX", "WOLF", "ZEBRA", "RABBIT", "DEER", "GIRAFFE",
"TIGER",
"LION", "GORILLA", "SNAKE", "CROCODILE", "TURTLE", "WHALE", "KANGAROO", "FROG", "DOLPHIN", "PANDA", "COW", "CAT",
"DOG",
"OX", "HORSE", "CAMEL", "GOAT", "DONKEY", "BULL", "SHEEP", "RAT", "SQUIRREL", "BAT", "HAMSTER", "CHIMPANZEE",
"LIZARD",
"GAZELLE", "HIPPO", "HYENA", "WHALE", "KOALA", "LLAMA", "POLARBEAR", "JAGUAR", "ORANGUTAN", "LEOPARD", "CHEETAH",
"LYNX", "PANTHER", "PUMA", "RACCOON", "REINDEER", "SEAL", "BEAVER", "BISON", "BLUEWHALE", "BOBCAT", "BUFFALO",
"COBRA",
"ALLIGATOR", "CHAMELEON", "LONDON", "PARIS", "ROME", "VIENNA", "NEWYORK", "WASHINGTON", "BERLIN", "HAMBURG",
"ISTANBUL",
"ANKARA", "ATHENS", "AMSTERDAM", "BARCELONA", "VENICE", "MOSCOW", "TOKYO", "CHICAGO", "GAZIANTEP", "SAMSUN",
"ARMCHAIR",
"BED", "LIBRARY", "CHAIR", "CLOCK", "CUPBOARD", "DESK", "MIRROR", "SOFA", "STOOL", "TABLE", "IRON", "LAMP",
"SMARTPHONE", "FRIDGE", "BOWL", "SPOON", "FORK", "KNIFE", "CARPET", "TOWEL", "DUSTBIN", "VASE", "CANDLE",
"SCISSORS",
"TOOTHPASTE", "AUTOMOBILE", "BAG", "BALL", "BASKET", "BELL", "BICYCLE", "BILL", "BOTTLE", "BOW", "BUTTON", "CAKE",
"CAMERA", "CANDY", "CAR", "CARD", "CARTON", "CHESS", "CHOCOLATE", "CLOTHES", "COMPUTER", "DESK", "DICE", "DOOR",
"DUSTER", "ELEVATOR", "ENGINE", "EYEGLASSES", "FAN", "FIDDLE", "FLAG", "GAME", "GOLD", "GUITAR", "GUN", "HAIRDRYER",
"HAT", "HOME", "JACKET", "JEEP", "KETTLE", "KEYBOARD", "LADDER", "LAPTOP", "LOCK", "MAGAZINE", "MONITOR",
"MOTORCYCLE",
"NEEDLE", "NEWSPAPER", "NYLON", "OVEN", "PAINT", "PAPER", "PENCIL", "POWDER", "RADIO", "RAZOR", "ROBOT", "RUG",
"SCARF",
"SCISSORS", "SEAT", "SHIP", "SHOE", "SHORTS", "TABLE", "TELEVISION", "TELEPHONE", "TRACTOR", "UMBRELLA", "VIOLIN",
"WALLET", "WEAPONS", "WHEEL", "WINDOW", "WORKBOX", "XEROX", "YARN", "ZIPPER", "ZEPPELIN")
if __name__ == '__main__':
while True:
word = random.choice(WORDS)
True_Answer = ("Well done!", "Superb!", "You Legend!!")
MAX_ERROR = len(HANGMAN)
so_far = ("-") * len(word)
letters_used = []
error = 0
print("\t \t \t 웃 Welcome to Hangman 웃")
print()
print("Words can be many things, for example; seasons, months, animals, cities, objects, names etc.")
print()
print("Therefore, You have the right to make 10 mistakes. Good luck!!")
print()
print("Please guess only 'LETTERS'")
print()
input("Press Enter to START: ")
while error < MAX_ERROR and so_far != word:
print()
print(HANGMAN[error])
print("Word to Predict: ", so_far)
print("Used Letters: ", letters_used)
guess = input("Guess a letter: ").upper()
sleep(1) # Time delay - allows userfriendly reading
print()
while guess in letters_used:
print("Try again... You've already used this letter")
guess = input("Guess a letter: ").upper()
sleep(1)
print()
letters_used.append(guess)
if guess in word:
print(random.choice(True_Answer), "...Updating word so far...")
new = ""
for i in range(len(word)):
if guess == word[i]:
new += guess
else:
new += so_far[i]
so_far = new
else:
print("INCORRECT! Try again!")
error += 1
print()
print("Calculating result ...")
print()
sleep(1)
if error == MAX_ERROR:
print("Correct Ansver=")
print(word)
print()
print("UNFORTUNATELY! The man was hanged. ☠ ")
else:
print("WINNER!♛ The man survived. ☺ ")
print("Correct Ansver=")
print(word)
print()
restart = input("Do you want to play again?(y or n)")
if restart.lower() != "y":
print("Have a good day. Game closing.")
break
else:
print("Let start again after 3 second!")
sleep(3)
clear_screen()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment