-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhangman.py
More file actions
63 lines (53 loc) · 1.82 KB
/
hangman.py
File metadata and controls
63 lines (53 loc) · 1.82 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
"""
Create a simple hangman game
"""
import random
# create word dictionary
three_letters = ['tie', 'gun', 'mat']
four_letters = ['fish', 'sack', 'jump', 'wind']
five_letters = ['phone', 'range', 'grill']
six_letters = ['turtle', 'bottle', 'tablet']
seven_letters = ['monitor', 'prepare', 'octopus']
word_list = three_letters + four_letters + five_letters + \
six_letters + seven_letters
# randomly select one word
# random.seed(9)
wordchoice = random.choice(word_list)
# print(wordchoice)
wordlen = len(wordchoice)
correct_guess = []
wrong_guess = []
tried_guess = []
print('** Game Start **')
print('It is a %d letters word: %s' % (wordlen, '_ '*wordlen))
while True:
guess = input('\nInput a letter:')
# validate user input is alphabet and single letter
guess = guess.lower()
if not guess.isalpha():
print('Input an alphabet!')
continue
elif len(guess) > 1:
print('Input a single letter!')
continue
# check if user guessed this letter before
if tried_guess.__contains__(guess):
print('You guessed that before')
continue
else:
tried_guess.append(guess)
# Check if letter is correct
if guess in wordchoice:
correct_guess.append(guess)
letter_pos = [i for i,letter in enumerate(wordchoice) if letter in correct_guess]
build_answer = [letter+' ' if i in letter_pos else '_ ' for i,letter in enumerate(wordchoice)]
print('Your word:', ''.join(build_answer))
if len(correct_guess) == len(set(wordchoice)):
print('You guess all correctly! Congratulation!')
else:
wrong_guess.append(guess)
print('Nope!! You have',5 - len(wrong_guess), 'tries left')
if (5 - len(wrong_guess)) == 0:
print('You run out of tries!')
break
print('** Game End **')