-
-
Notifications
You must be signed in to change notification settings - Fork 9
/
hangman.py
67 lines (53 loc) · 1.47 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
55
56
57
58
59
60
61
62
63
64
65
66
67
# This code is made by MRaynasim
import random
import time
def hangman():
words = [
"apple",
"banana",
"cherry",
"orange",
"strawberry",
"watermelon",
"pineapple",
"kiwi",
"mango",
"pear",
"grapefruit",
"blueberry",
]
word = random.choice(words)
guessed_letters = []
attempts = 5
while attempts > 0:
print("\nAttempts left:", attempts)
hidden_word = ""
for letter in word:
if letter in guessed_letters:
hidden_word += letter + " "
else:
hidden_word += "_ "
print(hidden_word)
if hidden_word == word:
print("Congratulations! You guessed the word:", word)
break
guess = input("Guess a letter: ").lower()
if len(guess) != 1:
print("Please enter a single letter.")
continue
if guess in guessed_letters:
print("You have already guessed that letter.")
continue
guessed_letters.append(guess)
if guess in word:
print("Correct guess!")
else:
print("Wrong guess!")
attempts -= 1
if attempts == 0:
print("Sorry, you ran out of attempts. The word was:", word)
# Run the Hangman game
print("Hello! This Hangman game is made by MRayan Asim. Hope you will enjoy it! 😊")
time.sleep(3)
print("Let's play Hangman!")
hangman()