-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPython-Hangman.py
More file actions
67 lines (53 loc) · 1.77 KB
/
Python-Hangman.py
File metadata and controls
67 lines (53 loc) · 1.77 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
import random
# List of words to guess
WORDS = ['python', 'hangman', 'programming', 'challenge', 'developer']
def get_word():
"""
Randomly selects a word from the list of words.
"""
return random.choice(WORDS)
def display_word(word, guesses):
"""
Displays the word with guessed letters and underscores for unknown letters.
Args:
word (str): The word to be guessed.
guesses (set): The set of guessed letters.
Returns:
str: The word with guessed letters and underscores.
"""
return ' '.join([letter if letter in guesses else '_' for letter in word])
def hangman():
"""
Runs the Hangman game.
"""
word = get_word()
guesses = set()
incorrect_guesses = set()
max_attempts = 6
attempts = 0
print("Welcome to Hangman!")
print(display_word(word, guesses))
while attempts < max_attempts:
guess = input("Guess a letter: ").lower()
if len(guess) != 1 or not guess.isalpha():
print("Please enter a single letter.")
continue
if guess in guesses or guess in incorrect_guesses:
print("You've already guessed that letter.")
continue
if guess in word:
guesses.add(guess)
print("Good guess!")
else:
incorrect_guesses.add(guess)
attempts += 1
print(f"Incorrect! You have {max_attempts - attempts} attempts left.")
current_display = display_word(word, guesses)
print(current_display)
if '_' not in current_display:
print("Congratulations, you won!")
break
else:
print(f"Game over! The word was '{word}'.")
if __name__ == "__main__":
hangman()