-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsimple_hangman.py
71 lines (52 loc) · 1.46 KB
/
simple_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
#!/usr/bin/env python3.8
'''
Hangman Game by Simon Holland / Inyoka (Basic)
'''
import random
import os
from levels import level_6 as words
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:
clear()
status()
myguess = input('Enter a letter : ').lower()
guess(myguess)
if won():
clear()
print(f'Congratulations, you won! The word was "{myword.upper()}"')
status()
else :
clear()
print(f'Sorry the word was {myword.upper()}')
status()
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 status():
print(f'Guesses left : {game_state[len(wrong_letters)]}')
print(f'Remaining : {hide_letters(myword)}')
if __name__ == '__main__':
main()