-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHangman.py
More file actions
118 lines (109 loc) · 2.15 KB
/
Hangman.py
File metadata and controls
118 lines (109 loc) · 2.15 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
import random
# import only system from os
from os import system, name
stages = ['''
+---+
| |
O |
/|\ |
/ \ |
|
=========
''', '''
+---+
| |
O |
/|\ |
/ |
|
=========
''', '''
+---+
| |
O |
/|\ |
|
|
=========
''', '''
+---+
| |
O |
/| |
|
|
=========''', '''
+---+
| |
O |
| |
|
|
=========
''', '''
+---+
| |
O |
|
|
|
=========
''', '''
+---+
| |
|
|
|
|
=========
''']
logo = '''
_
| |
| |__ __ _ _ __ __ _ _ __ ___ __ _ _ __
| '_ \ / _` | '_ \ / _` | '_ ` _ \ / _` | '_ \
| | | | (_| | | | | (_| | | | | | | (_| | | | |
|_| |_|\__,_|_| |_|\__, |_| |_| |_|\__,_|_| |_|
__/ |
|___/ '''
# define our clear function
def clear():
# for windows
if name == 'nt':
_ = system('cls')
# for mac and linux(here, os.name is 'posix')
else:
_ = system('clear')
print(logo)
chosen_word = input("shhh input the secret word!\n")
display_list = []
word_length = len(chosen_word)
print(f"The chosen word is {chosen_word}")
for blank in range(word_length):
display_list += "_"
print(display_list)
end = False
lives_left = 6
while not end:
guess = input("Guess a letter\n").lower()
# clear()
if guess in display_list:
print(f"You've already guessed {guess}, try another letter.")
# check guessed letter
for position in range(word_length):
letter = chosen_word[position]
if letter == guess:
display_list[position] = letter
print(display_list)
# check for wrong guess
if guess not in chosen_word:
lives_left -= 1
print(f"You guessed {guess}, which is wrong. You loose a life. \nLives left : {lives_left}")
if lives_left == 0:
end = True
print("You lose!")
# check if word is guessed if the blanks are cleared
if "_" not in display_list:
end = True
print("You win!")
print(stages[lives_left])