-
Notifications
You must be signed in to change notification settings - Fork 0
/
hangman_game
62 lines (59 loc) · 1.95 KB
/
hangman_game
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
import time
import random
class HangMan(object):
words = '''ant cat car'''.split()
def __init__(self):
pass
def pickWord(self):
return self.words[random.randint(0, len(self.words) - 1)]
def askAndEvaluate(self, word, result, missed):
guess = input()
if guess == None or len(guess) != 1 or (guess in result) or (guess in missed):
return None, False
i = 0
right = guess in word
for c in word:
if c == guess:
result[i] = c
i += 1
return guess, right
def start(self):
word = list(self.pickWord())
result = list('*' * len(word))
print(word)
print('The word is: ', result)
success, i, missed = False, 0, []
while i < (len(word) +2):
print('Guess the word: ', end='')
guess,right = self.askAndEvaluate(word, result, missed)
if guess == None:
print('You\'ve already entered this character.')
continue
print(''.join(result))
if result == word:
print('Congratulations ! You\'ve just saved a life !')
success = True
break
if not right:
missed.append(guess)
i+=1
print('Missed characters: ', missed)
if not success:
print('The word was \''+''.join(word)+'\' ! You not able to save yourself')
print("\t\t +-----+")
print("\t\t | |")
print("\t\t o |")
print("\t\t/|\\ |")
print("\t\t | |")
print("\t\t/ \\ |")
print("\t\t |")
print("\t\t=========")
name =input("Your name: ")
print("Hi! {} lets play Hangnan game".format(name))
time.sleep(1)
print("Here try to safe yourself by guessing correct letter")
print("\t\t o")
print("\t\t/|\\")
print("\t\t |")
print("\t\t/ \\")
a = HangMan().start()