-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhangman.py
54 lines (43 loc) · 1.3 KB
/
hangman.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
import random
def get_word():
"""Selects a random word from a list."""
words = ["python", "hangman", "programming", "computer", "science"]
return random.choice(words)
def display_board(word_guess, guessed_letters):
"""Displays the current state of the game."""
print("Current word:", end=" ")
for letter in word_guess:
if letter in guessed_letters:
print(letter, end=" ")
else:
print("_", end=" ")
print()
def play_hangman():
"""Main game loop."""
word = get_word()
word_guess = "_" * len(word)
guessed_letters = []
attempts = 6
while attempts > 0:
display_board(word_guess, guessed_letters)
guess = input("Guess a letter: ").lower()
if len(guess) != 1 or not guess.isalpha():
print("Invalid input. Please enter a single letter.")
continue
if guess in guessed_letters:
print("You already guessed that letter.")
continue
guessed_letters.append(guess)
if guess in word:
print("Correct!")
word_guess = "".join([letter if letter in guessed_letters else "_" for letter in word])
else:
print("Incorrect!")
attempts -= 1
if word_guess == word:
print("Congratulations! You won!")
break
if attempts == 0:
print("You lost! The word was:", word)
if __name__ == "__main__":
play_hangman()