-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtask1.py
More file actions
157 lines (128 loc) · 3.22 KB
/
Copy pathtask1.py
File metadata and controls
157 lines (128 loc) · 3.22 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
151
152
153
154
155
156
157
import random
# HANGMAN GAME — CodeAlpha Internship Task 1#
WORDS = ["python", "hangman", "laptop", "science", "program"]
# Hangman visual stages (0 = no mistakes, 6 = game over)
HANGMAN_STAGES = [
"""
-----
| |
O |
|
|
|
=========
""",
"""
-----
| |
O |
|
|
|
=========
""",
"""
-----
| |
O |
| |
|
|
=========
""",
"""
-----
| |
O |
/| |
|
|
=========
""",
"""
-----
| |
O |
/|\ |
|
|
=========
""",
"""
-----
| |
O |
/|\ |
/ |
|
=========
""",
"""
-----
| |
O |
/|\ |
/ \ |
|
=========
"""
]
def display_word(word, guessed_letters):
"""Display the word with guessed letters revealed."""
display = ""
for letter in word:
if letter in guessed_letters:
display += letter + " "
else:
display += "_ "
return display.strip()
def hangman():
"""Main Hangman game function."""
print("=" * 40)
print(" Welcome to HANGMAN! 🎮")
print("=" * 40)
word = random.choice(WORDS)
guessed_letters = set()
incorrect_guesses = 0
max_incorrect = 6
print(f"\nGuess the word! It has {len(word)} letters.\n")
while incorrect_guesses < max_incorrect:
print(HANGMAN_STAGES[incorrect_guesses])
# Show current word state
print(f"Word: {display_word(word, guessed_letters)}")
print(f"Wrong guesses left: {max_incorrect - incorrect_guesses}")
print(f"Guessed letters: {', '.join(sorted(guessed_letters)) if guessed_letters else 'None'}")
print("-" * 40)
guess = input("Enter a letter: ").lower().strip()
if len(guess) != 1 or not guess.isalpha():
print("⚠️ Please enter a single letter!\n")
continue
if guess in guessed_letters:
print(f"⚠️ You already guessed '{guess}'. Try a different letter!\n")
continue
guessed_letters.add(guess)
if guess in word:
print(f"✅ Great! '{guess}' is in the word!\n")
# Check if player has won
if all(letter in guessed_letters for letter in word):
print(HANGMAN_STAGES[incorrect_guesses])
print(f"🎉 YOU WIN! The word was: '{word.upper()}'")
print("=" * 40)
return
else:
incorrect_guesses += 1
print(f"❌ Wrong! '{guess}' is not in the word.\n")
print(HANGMAN_STAGES[max_incorrect])
print(f"💀 GAME OVER! The word was: '{word.upper()}'")
print("=" * 40)
def main():
while True:
hangman()
print()
play_again = input("Play again? (yes/no): ").lower().strip()
if play_again not in ("yes", "y"):
print("\nThanks for playing! Goodbye 👋")
break
print()
if __name__ == "__main__":
main()