-
Notifications
You must be signed in to change notification settings - Fork 0
/
Hangman Game.py
167 lines (139 loc) · 4.98 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
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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
import random
import openpyxl
import os
filepath="C:/Users/MUHAMMAD HASNAIN/Desktop/project/data.xlsx"
if not os.path.exists(filepath):
workbook = openpyxl.Workbook()
sheet = workbook.active
heading = ["Name","Status"]
sheet.append(heading)
workbook.save(filepath)
def hangman():
list_of_words=['eduroam','hangman','pakistan','laptop']
word=random.choice(list_of_words)
turns=10
guessmade=''
valid_entry=set('abcdefghijklmnopqrstuvwxyz')
while len(word)>0:
main_word=''
for letter in word:
if letter in guessmade:
main_word = main_word + letter
else:
main_word = main_word + '_ '
if main_word == word:
print(main_word)
print('you won!!!')
workbook = openpyxl.load_workbook(filepath)
sheet = workbook.active
a=[name,"Congratulation! You won"]
sheet.append(a)
workbook.save(filepath)
break
print('guess the word',main_word)
print()
guess=input()
if guess in valid_entry:
guessmade = guessmade + guess
else:
print('Enter a valid character')
guess=input()
print()
if guess not in word:
turns=turns-1
if turns == 9:
print('9 turns left!!')
print('-----------------------------')
print('|')
print('|')
print('|')
print('|_______')
print('| |')
if turns == 8:
print('8 turns left!!')
print('-----------------------------')
print('_____')
print('|')
print('|')
print('|_________')
print('| |')
if turns == 7:
print('7 turns left!!')
print('-----------------------------')
print('___')
print('| O')
print('| ')
print('|___________')
print('| |')
if turns == 6:
print('6 turns left!!')
print('-----------------------------')
print('___')
print('| O')
print('| | ')
print('|_____/_______')
print('| |')
if turns == 5:
print('5 turns left!!')
print('-----------------------------')
print('___')
print('| O')
print('| | ')
print('|_____/_\_____')
print('| |')
if turns == 4:
print('4 turns left!!')
print('___')
print('| \O')
print('| | ')
print('|_____/_\_____')
print('| |')
if turns == 3:
print('3 turns left!!')
print('___')
print('| \O/')
print('| | ')
print('|_____/_\_____')
print('| |')
if turns == 2:
print('2 turns left!!')
print('-----------------------------')
print('___')
print('| | \O/')
print('| | ')
print('|_____/_\_____')
print('| |')
if turns == 1:
print('1 turns left!!! Hangman on his last breath')
print('-----------------------------')
print('___')
print('| |__\O/')
print('| | ')
print('|_____/_\_____')
print('| |')
if turns == 0:
print('Sorry! You loose')
print('You let a good man die')
print('-----------------------------')
print('___')
print('| |')
print('| |__O')
print('|____/|\_____')
print('| /|\ |')
workbook = openpyxl.load_workbook(filepath)
sheet = workbook.active
b=[name,"you lose"]
sheet.append(b)
workbook.save(filepath)
break
while True:
name = input('Enter a player name --> ')
print('Welcome',name,'!')
print('-------------------------')
print('Try to guess the word in 10 attempts')
hangman()
user_input = input("Do you want to play again? (y/n)")
if user_input.lower() == 'n':
print("Thank you, Have a nice day!")
break
print("Enter a valid Charater")