-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhangman.py
More file actions
86 lines (63 loc) · 1.76 KB
/
Copy pathhangman.py
File metadata and controls
86 lines (63 loc) · 1.76 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
#!/usr/bin/env python3.8
'''
Hangman Game by Simon Holland / Inyoka (Advanced)
'''
import random
import os
import re
import time
from levels import level_7 as words
from gallows import gallows
myword = random.choice(words)
game_state = list(reversed(range(0,7)))
guessed_letters, wrong_letters = [], []
def main():
while not won() and game_state[len(wrong_letters)] != 0:
run()
if won():
clear()
status()
print(f'Congratulations, you won! The word was "{myword.upper()}"\n')
else :
clear()
status()
print(f'Sorry the word was {myword.upper()}\n')
def clear():
os.system('cls' if os.name == 'nt' else 'clear')
def won():
if ' _ ' not in hide_letters(myword):
return True
return False
def guess(letter):
if letter in myword and letter not in guessed_letters:
guessed_letters.append(letter)
elif letter not in myword and letter not in wrong_letters:
wrong_letters.append(letter)
else:
return False
return True
def hide_letters(word):
hidden_letters = ''
for letter in word:
if letter not in guessed_letters:
hidden_letters += ' _ '
else:
hidden_letters += f' {letter} '
return hidden_letters
def show_letters(wrong_letters):
return ', '.join(wrong_letters)
def status():
print(f'Guesses left : {game_state[len(wrong_letters)]}\n')
print(f'Mistakes : {show_letters(wrong_letters)}')
print(gallows[len(wrong_letters)])
print(f'Remaining : {hide_letters(myword)}\n')
def run():
clear()
status()
myguess = input('Enter a letter : ').lower()
if re.match('[a-z]', myguess) :
guess(myguess[0])
else :
run()
if __name__ == '__main__':
main()