-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhangman.py
More file actions
88 lines (80 loc) · 1.68 KB
/
hangman.py
File metadata and controls
88 lines (80 loc) · 1.68 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
import random
# Hangman visual stages
stages = [
'''
+---+
| |
|
|
|
|
=========''', '''
+---+
| |
O |
|
|
|
=========''', '''
+---+
| |
O |
| |
|
|
=========''', '''
+---+
| |
O |
/| |
|
|
=========''', '''
+---+
| |
O |
/|\\ |
|
|
=========''', '''
+---+
| |
O |
/|\\ |
/ |
|
=========''', '''
+---+
| |
O |
/|\\ |
/ \\ |
|
========='''
]
# Word list
word_list = ['banana', 'mango', 'apple', 'orange', 'kiwi', 'grape', 'papaya', 'pineapple', 'blueberry', 'strawberry', 'watermelon', 'peach', 'apricot' ,'book', 'pencil', 'school', 'window', 'ocean', 'planet', 'mountain', 'castle', 'rocket', 'forest', 'desert', 'bottle', 'guitar'
]
chosen_word = random.choice(word_list)
display = ['_' for _ in chosen_word]
lives = 6
gameover = False
print("🎮 Welcome to Hangman!")
print(display)
while not gameover:
guess = input("Enter a letter: ").lower()
if guess in chosen_word:
for index, letter in enumerate(chosen_word):
if letter == guess:
display[index] = guess
else:
lives -= 1
print(f"❌ Incorrect! Lives remaining: {lives}")
if lives == 0:
gameover = True
print("💀 You lose! The word was:", chosen_word)
print(display)
print(stages[6 - lives])
if '_' not in display:
gameover = True
print("🎉 You win!")