-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathP3_Hangman_game.py
More file actions
143 lines (118 loc) · 2.97 KB
/
P3_Hangman_game.py
File metadata and controls
143 lines (118 loc) · 2.97 KB
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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
from random import choice
import time
mystery_words = [
"python",
"javascript",
"fullstack",
"function",
"variable",
"algorithm",
"data",
"structure",
"object",
"oriented",
]
def hangman_game(attempt):
step = [
"""
-------
| |
|
|
|
|
============
""",
"""
-------
| |
O |
|
|
|
============
""",
"""
-------
| |
O |
| |
|
|
============
""",
"""
-------
| |
O |
/| |
|
|
============
""",
"""
-------
| |
O |
/|\\ |
|
|
============
""",
"""
-------
| |
O |
/|\\ |
/ \\ |
|
===========
""",
]
print(step[attempt])
# main function
def main():
start_time = time.time()
print("{:=^70}".format("WELCOME TO HANGMAN GAME "))
dice_choice = choice(mystery_words)
hidden_word = ["*"] * len(dice_choice)
health = 6
already_gess = set()
attempt = 0
while health > 0:
print(f" Hidden word: {"".join(hidden_word)}")
print(f"Health left: {health}")
print(
f"Already guessed :{",".join(sorted(already_gess)) if already_gess else "None"}"
)
hangman_game(attempt)
guess = input("Guess letters of mystery Word: ").lower()
if len(guess) != 1 or not guess.isalpha():
print("Error: you must enter exactly 1 letter")
continue
if guess in already_gess:
print("You already guess this letter")
continue
already_gess.add(guess)
# win condition
if guess in dice_choice:
print("well done!! you are find one letter")
for i, letter in enumerate(dice_choice):
if guess == letter:
hidden_word[i] = guess
else:
print("Oups!! Wrong gess")
attempt += 1
health -= 1
if "*" not in hidden_word:
elapsed_time = time.time() - start_time
print(f"Well done!! you are win in {elapsed_time:.2f} second")
print(f"the secret word is: {dice_choice}")
break
if health == 0:
elapsed_time = time.time() - start_time
print(f"Oups!! you lost the time taken is {elapsed_time:.2f} second")
print(f"the secret word is: {dice_choice}")
break
if __name__ == "__main__":
main()