generated from Code-Institute-Org/p3-template
-
Notifications
You must be signed in to change notification settings - Fork 1
/
run.py
195 lines (155 loc) · 5.24 KB
/
run.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
import random
import sys
import os
from countries import country_list
from food_and_drink import fd_list
from technology import tech_list
from gallows import art, title
def clear():
"""
Clears the screen
"""
if os.name == 'nt':
os.system('cls')
else:
os.system('clear')
def welcome_player():
"""
Welcomes the user, asks to select a category and
checks whether they are happy with their decision
"""
print("Welcome to Hangman\n")
name = input("Please Enter your Name: ").capitalize()
while True:
category = select_category()
decision = input(f"{name}, you have six lives and have selected {category}. \nAre you happy with your choice? (Y/N) ").upper()
if decision == "Y":
print("Let's play. Good luck!\n")
break
elif decision == "N":
clear()
else:
print("Invalid input. Please enter 'Y' or 'N'.\n")
return category
def select_category():
"""
Prompts the user to select a category from a list.
"""
while True:
try:
selection = int(input("\nPlease select a category:\n1. Countries\n2. Technology\n3. Food and Drink\n\n"))
if 1 <= selection <= 3:
break
else:
print("Invalid entry. Please select a valid category.\n")
except ValueError:
print("Please enter a number between 1 and 3.\n")
if selection == 1:
category = "Countries"
elif selection == 2:
category = "Technology"
else:
category = "Food and Drink"
return category
chosen_category = welcome_player()
def play_game():
"""
Starts the game of Hangman
"""
# Clear screen for better user visibility
clear()
random_word = ""
print(title)
print()
print("HINT: some words may contain spaces")
print()
print(art[6])
# Generates a random word based on the category selected
if chosen_category == "Countries":
random_word = random.choice(country_list)
elif chosen_category == "Technology":
random_word = random.choice(tech_list)
else:
random_word = random.choice(fd_list)
word_length = len(random_word)
hidden_word = "-" * word_length
wrong_letters = ""
guessed_letters = []
while True:
# Gallows and word to guess
print(chosen_category)
print()
print(hidden_word)
letter = input("\nPlease enter a letter: ").upper()
if letter in random_word:
temp = ""
for index in range(len(random_word)):
if letter == random_word[index]:
temp += letter
clear()
print(art[6])
print(f"\nLetters Guessed: {''.join(guessed_letters)}")
print(f"\nIncorrect Letters: {wrong_letters}")
elif hidden_word[index] != "-":
temp += hidden_word[index]
else:
temp += "-"
hidden_word = temp
else:
wrong_letters += letter
guessed_letters.append(letter)
# print(f"\nLetters Guessed: {''.join(guessed_letters)}")
# print(f"\nIncorrect Letters: {wrong_letters}")
# Gallows and number of attempts
if len(wrong_letters) == 1:
clear()
print(f"\nLetters Guessed: {''.join(guessed_letters)}")
print(f"\nIncorrect Letters: {wrong_letters}")
print(art[5])
if len(wrong_letters) == 2:
clear()
print(f"\nLetters Guessed: {''.join(guessed_letters)}")
print(f"\nIncorrect Letters: {wrong_letters}")
print(art[4])
if len(wrong_letters) == 3:
clear()
print(f"\nLetters Guessed: {''.join(guessed_letters)}")
print(f"\nIncorrect Letters: {wrong_letters}")
print(art[3])
if len(wrong_letters) == 4:
clear()
print(f"\nLetters Guessed: {''.join(guessed_letters)}")
print(f"\nIncorrect Letters: {wrong_letters}")
print(art[2])
if len(wrong_letters) == 5:
clear()
print(f"\nLetters Guessed: {''.join(guessed_letters)}")
print(f"\nIncorrect Letters: {wrong_letters}")
print(art[1])
if len(wrong_letters) == 6:
# Clear screen before asking about retry
clear()
print(art[0])
print("You're Dead")
print(f"The word was {random_word}")
retry = input("\nTry Again? Y/N ").upper()
if retry == "Y":
clear()
play_game()
else:
clear()
print("\nCome back when you're brave enough!!")
exit()
if "-" not in hidden_word:
clear()
print(random_word)
print(art[7])
play_again = input("\nCongratulations. You Survived. Play Again? Y/N ").upper()
if play_again == "Y":
clear()
play_game()
else:
clear()
print("Thanks for playing.")
exit()
play_game()