-
Notifications
You must be signed in to change notification settings - Fork 0
/
hangman.py
215 lines (195 loc) · 5.02 KB
/
hangman.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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
#Hangman Game
import os, platform
from random import *
import sys
player_score = 0
computer_score = 0
def clear():
print(platform.system())
if platform.system() == "Windows":
os.system('cls')
else:
os.system('clear')
def hangedman(hangman):
graphic = [
"""
+---------------+
|
|
|
|
|
|
|
|
|
|
=======================
""",
"""
+---------------+
| |
| O
|
|
|
|
|
|
|
|
=======================
""",
"""
+---------------+
| |
| O
| |
|
|
|
|
|
|
|
=======================
""",
"""
+---------------+
| |
| O
| --|
|
|
|
|
|
|
|
=======================
""",
"""
+---------------+
| |
| O
| --|--
|
|
|
|
|
|
|
=======================
""",
"""
+---------------+
| |
| O
| --|--
| |
|
|
|
|
|
|
=======================
""",
"""
+---------------+
| |
| O
| --|--
| | |
|
|
|
|
|
|
=======================
""",
]
print(graphic[hangman])
return
def start():
clear()
print("Let's play a game of Linux Hangman.")
while game():
pass
scores()
def game():
try:
category = int(input("1-Physics\nMore Category adding Soon\nEnter the category :"))
except:
print("Invalid Input")
sys.exit()
dictionary = {1:'physics.txt'}
file = open(dictionary[category],'r')
all_lines = file.readlines()
number_of_lines = len(all_lines)
l = randint(1,number_of_lines)
word = all_lines[l-1]
word = word.split('\n')[0]
word_length = len(word)
clue = word_length*["_"]
tries = 6
letters_tried = ''
guesses = 0
letters_right = 0
letters_wrong = 0
global computer_score, player_score
while (letters_wrong != tries) and ("".join(clue) != word):
letter = guess_letter()
if len(letter) == 1 and letter.isalpha():
if letters_tried.find(letter) != -1:
print("You've alread picked",letter)
else:
letters_tried = letters_tried + letter
first_index = word.find(letter)
if first_index == -1:
letters_wrong += 1
print("Sorry",letter,"isn't what we're looking for.")
else:
print("Congo",letter,"is correct.")
for i in range(word_length):
if letter == word[i]:
clue[i]=letter
else:
print("Choose another")
hangedman(letters_wrong)
print(" ".join(clue))
print("Guesses: ",letters_tried)
if letters_wrong == tries:
print("Game Over")
print("The word was",word)
computer_score += 1
break
if ''.join(clue) == word:
print("You Win!")
print("The word was",word)
player_score += 1
break
return play_again()
def guess_letter():
letter = input("Take a guess at our mystery word: ")
letter.strip()
letter.lower()
print()
clear()
return letter
def play_again():
answer = input("Would you like to play again? Press any key to continue except q to exit:")
if answer.lower() == 'q':
print("Thank you very much for playing our game. See you next time.")
return False
clear()
return True
def scores():
global player_score, computer_score
print("HIGH SCORES")
print("Player: ",player_score)
print("Computer: ",computer_score)
if __name__ == "__main__":
start()