-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhangman game.py
106 lines (90 loc) · 2.72 KB
/
hangman game.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
"""
Date: 10 jan 2021
Author: Sanam Kandar
Project: Hangman
"""
import random
import datetime
print("\t\t\t\t\t\t♦ WELCOME TO THE HANGMAN GAME ♦")
print("\t\t\t\t\t♦ Guess super-hero from Marvels & DC ♦\n")
superheros = ["ironman", "hawkeye", "thor", "aquaman", "hulk", "groot",
"doctorstrange", "galmora", "spiderman", "captainamerica"]
# this function returns a super-hero name
def random_hero(superheroslist):
return random.choice(superheroslist)
HANGMAN = ['''
+---+
|
|
|
===''', '''
+---+
O |
|
|
===''', '''
+---+
O |
| |
|
===''', '''
+---+
O |
/| |
|
===''', '''
+---+
O |
/|\ |
|
===''', '''
+---+
O |
/|\ |
/ |
===''', '''
+---+
O |
/|\ |
/ \ |
===''']
turns = True
while turns: # user enter the loop and enter name
secret_hero = random_hero(superheros)
name = input("Enter your name: \n")
print(f"Best of luck {name} !\n")
turns = False
time1 = datetime.datetime.now() # time starts to guess
trail = 0
fill_word = "" # empty str
words = set() # empty set
while trail <= 6:
print(HANGMAN[0 + trail]) # initializing HANGMAN
if trail == 6:
print(f"You ran your trails, better luck next time! \nThe word was {secret_hero}.")
exit()
"""
comparing char in secret hero with char in fill word,
only present char are adding to words(a empty list).
"""
for letter in secret_hero:
if letter in fill_word:
words.add(letter)
print(letter)
else:
print("_")
# if len of words and secret hero is same, then consider user as a winner with time he\she took.
if len(words) == len(set(secret_hero)):
time2 = datetime.datetime.now()
print(f"Congrats you got the word! '{secret_hero}'. "
f"You took {time2-time1} time to guess. \nYOU'RE WINNER! ")
exit()
user_guess = input("Please guess a letter: ").lower()
"""
if user guess (letter or char) is not in secret hero,
initialize trail.
"""
if user_guess not in secret_hero:
trail += 1
print(f"wrong! {user_guess} is not in word. ")
fill_word += user_guess # initializing user guess to a empty str(fill word).