-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path02_Hangman_Game.py
More file actions
150 lines (135 loc) · 3.46 KB
/
Copy path02_Hangman_Game.py
File metadata and controls
150 lines (135 loc) · 3.46 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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
import random
# Hangman ASCII Art
HANGMAN_PICS = [
"""
-----
| |
|
|
|
|
=========""",
"""
-----
| |
O |
|
|
|
=========""",
"""
-----
| |
O |
| |
|
|
=========""",
"""
-----
| |
O |
/| |
|
|
=========""",
"""
-----
| |
O |
/|\\ |
|
|
=========""",
"""
-----
| |
O |
/|\\ |
/ |
|
=========""",
"""
-----
| |
O |
/|\\ |
/ \\ |
|
=========""",
]
# Word categories
WORD_CATEGORIES = {
"Animals": ["tiger", "elephant", "giraffe", "kangaroo", "dolphin"],
"Countries": ["canada", "germany", "india", "japan", "brazil"],
"Movies": ["inception", "gladiator", "matrix", "avatar", "titanic"]
}
# Function to choose a category
def choose_category():
print("Choose a category:")
categories = list(WORD_CATEGORIES.keys())
for idx, category in enumerate(categories):
print(f"{idx + 1}. {category}")
choice = int(input("Enter the number of your choice: ")) - 1
return categories[choice]
# Function to set the difficulty level
def set_difficulty():
print("Choose a difficulty level:")
print("1. Easy (10 guesses)")
print("2. Medium (7 guesses)")
print("3. Hard (5 guesses)")
choice = int(input("Enter the number of your choice: "))
if choice == 1:
return 10
elif choice == 2:
return 7
else:
return 5
# Function to give a hint
def give_hint(word, correct_letters):
for letter in word:
if letter not in correct_letters:
print(f"Hint: The word contains the letter '{letter}'")
return
# Main Hangman function
def hangman():
category = choose_category()
word = random.choice(WORD_CATEGORIES[category])
max_guesses = set_difficulty()
guessed = False
guessed_letters = []
correct_letters = []
incorrect_guesses = 0
print(f"\nThe category is: {category}")
print("_ " * len(word))
while not guessed and incorrect_guesses < max_guesses:
guess = input("\nEnter a letter or type 'hint' for a hint: ").lower()
if guess == "hint":
give_hint(word, correct_letters)
max_guesses -= 1
print(f"You have {max_guesses - incorrect_guesses} guesses left.")
continue
if len(guess) == 1 and guess.isalpha():
if guess in guessed_letters:
print("You already guessed that letter.")
elif guess not in word:
print(f"{guess} is not in the word.")
incorrect_guesses += 1
guessed_letters.append(guess)
else:
print(f"Good guess! {guess} is in the word.")
guessed_letters.append(guess)
correct_letters.append(guess)
else:
print("Invalid input. Please enter a single letter.")
current_word = [letter if letter in correct_letters else "_" for letter in word]
print(" ".join(current_word))
print(HANGMAN_PICS[incorrect_guesses])
if "_" not in current_word:
guessed = True
if guessed:
print(f"\nCongratulations! You guessed the word '{word}'!")
else:
print(f"\nSorry, you ran out of guesses. The word was '{word}'.")
# Run the Hangman game
hangman()