-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhangman.py
More file actions
48 lines (38 loc) · 1.36 KB
/
hangman.py
File metadata and controls
48 lines (38 loc) · 1.36 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
import random
# List of predefined words
word_list = ['apple', 'robot', 'chair', 'clock', 'plant']
selected_word = random.choice(word_list)
# Game setup
guessed_letters = []
incorrect_guesses = 0
max_incorrect = 6
# Display setup
display_word = ['_' for _ in selected_word]
print("Welcome to Hangman!")
print("Guess the word, one letter at a time.")
while incorrect_guesses < max_incorrect and '_' in display_word:
print("\nWord to guess: " + ' '.join(display_word))
guess = input("Enter a letter: ").lower()
# Input validation
if not guess.isalpha() or len(guess) != 1:
print("Please enter a single valid letter.")
continue
if guess in guessed_letters:
print("You already guessed that letter.")
continue
guessed_letters.append(guess)
# Check guess and update display
if guess in selected_word:
print("Correct!")
# Reveal all positions where the letter matches
for i in range(len(selected_word)):
if selected_word[i] == guess:
display_word[i] = guess
else:
incorrect_guesses += 1
print(f"Wrong! You have {max_incorrect - incorrect_guesses} guesses left.")
# Final result
if '_' not in display_word:
print("\n🎉 Congratulations! You guessed the word: " + selected_word)
else:
print("\n❌ Game over! The word was: " + selected_word)