-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDay7Hangman.py
More file actions
129 lines (105 loc) · 2.47 KB
/
Day7Hangman.py
File metadata and controls
129 lines (105 loc) · 2.47 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
119
120
121
122
123
124
125
126
127
128
import random
# Hangman
words_list = ['hallucination', 'lucid dream', 'procrastination', 'banana', 'apple', 'orange']
word_to_guess = random.choice(words_list)
word_length = len(word_to_guess)
# Game loop
trials = 6
game_won = False
shadow_word = []
unique_letters = []
letters_guessed = []
letters_used = []
# create shadow word
for char in word_to_guess:
if char == " ":
shadow_word.append(" ")
else:
shadow_word.append("_")
if unique_letters.count(char) == 0 and char != " ":
unique_letters.append(char)
def print_hang_man(number_of_trials):
if number_of_trials == 6:
print('''
______
|
|
|
|
---------''')
elif number_of_trials == 5:
print('''
______
| |
o |
|
|
---------''')
elif number_of_trials == 4:
print('''
______
| |
o |
| |
|
---------''')
elif number_of_trials == 3:
print('''
______
| |
o |
/ | |
|
---------''')
elif number_of_trials == 2:
print('''
______
| |
o |
/ | \\ |
|
---------''')
elif number_of_trials == 1:
print('''
______
| |
o |
/ | \\ |
/ |
---------''')
elif number_of_trials == 0:
print('''
______
| |
o |
/ | \\ |
/\ |
---------''')
while not game_won and trials > 0:
print_hang_man(trials)
print("Trials: ", trials)
print("Letters used: ", ", ".join(letters_used))
print("Word to Guess: ", "".join(shadow_word))
user_letter = str.lower(input("Guess a letter: "))
if letters_guessed.count(user_letter) > 0:
print("Letter already guessed, use another letter")
continue;
letters_used.append(user_letter)
letter_found = False
for index, char in enumerate(word_to_guess):
if user_letter != ' ' and user_letter == char:
letter_found = True
# reveal the character
shadow_word[index] = char
# updated guessed characters
if letters_guessed.count(user_letter) == 0:
letters_guessed.append(user_letter)
if not letter_found:
trials -= 1
if len(letters_guessed) == len(unique_letters):
game_won = True
if game_won:
print("You Won! The word is: ", "".join(shadow_word))
else:
print_hang_man(trials)
print("You lost! try again.")