-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathhangman_game.py
138 lines (117 loc) · 3.76 KB
/
hangman_game.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
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
'''
Hangman Game
Created By Abdullah AL-Yamani
'''
import random
import time
import os
def play_again():
question = 'Do You want to play again? y = yes, n = no \n'
play_game = input(question)
while play_game.lower() not in ['y', 'n']:
play_game = input(question)
if play_game.lower() == 'y':
return True
else:
return False
def hangman(word):
display = '_' * len(word)
count = 0
limit = 5
letters = list(word)
guessed = []
while count < limit:
guess = input(f'Hangman Word: {display} Enter your guess: \n').strip()
while len(guess) == 0 or len(guess) > 1:
print('Invalid input. Enter a single letter\n')
guess = input(
f'Hangman Word: {display} Enter your guess: \n').strip()
if guess in guessed:
print('Oops! You already tried that guess, try again!\n')
continue
if guess in letters:
letters.remove(guess)
index = word.find(guess)
display = display[:index] + guess + display[index + 1:]
else:
guessed.append(guess)
count += 1
if count == 1:
time.sleep(1)
print(' _____ \n'
' | \n'
' | \n'
' | \n'
' | \n'
' | \n'
' | \n'
'__|__\n')
print(f'Wrong guess: {limit - count} guesses remaining\n')
elif count == 2:
time.sleep(1)
print(' _____ \n'
' | | \n'
' | | \n'
' | \n'
' | \n'
' | \n'
' | \n'
'__|__\n')
print(f'Wrong guess: {limit - count} guesses remaining\n')
elif count == 3:
time.sleep(1)
print(' _____ \n'
' | | \n'
' | | \n'
' | | \n'
' | \n'
' | \n'
' | \n'
'__|__\n')
print(f'Wrong guess: {limit - count} guesses remaining\n')
elif count == 4:
time.sleep(1)
print(' _____ \n'
' | | \n'
' | | \n'
' | | \n'
' | O \n'
' | \n'
' | \n'
'__|__\n')
print(f'Wrong guess: {limit - count} guesses remaining\n')
elif count == 5:
time.sleep(1)
print(' _____ \n'
' | | \n'
' | | \n'
' | | \n'
' | O \n'
' | /|\ \n'
' | / \ \n'
'__|__\n')
print('Wrong guess. You\'ve been hanged!!!\n')
print(f'The word was: {word}')
if display == word:
print(f'Congrats! You have guessed the word \'{word}\' correctly!')
break
def play_hangman():
print('\nWelcome to Hangman\n')
name = input('Enter your name: ')
print(f'Hello {name}! Best of Luck!')
time.sleep(1)
print('The game is about to start!\nLet\'s play Hangman!')
time.sleep(1)
os.system('cls' if os.name == 'nt' else 'clear')
words_to_guess = [
'plants', 'hello', 'world'
]
play = True
while play:
word = random.choice(words_to_guess)
hangman(word)
play = play_again()
print('Thanks For Playing! We expect you back again!')
exit()
if __name__ == '__main__':
play_hangman()