Skip to content

Commit ed32ae2

Browse files
authored
Create Exercise-31-Guess-Letters.py
1 parent 177cd36 commit ed32ae2

File tree

1 file changed

+121
-0
lines changed

1 file changed

+121
-0
lines changed

Exercise-31-Guess-Letters.py

+121
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,121 @@
1+
'''
2+
Exercise 31: Guess Letters
3+
This exercise is Part 2 of 3 of the Hangman exercise series.
4+
The other exercises are: Part 1 and Part 3.
5+
6+
Let’s continue building Hangman. In the game of Hangman, a
7+
clue word is given by the program that the player has to
8+
guess, letter by letter. The player guesses one letter at a
9+
time until the entire word has been guessed. (In the actual
10+
game, the player can only guess 6 letters incorrectly before
11+
losing).
12+
13+
Let’s say the word the player has to guess is “EVAPORATE”.
14+
For this exercise, write the logic that asks a player to guess
15+
a letter and displays letters in the clue word that were
16+
guessed correctly. For now, let the player guess an infinite
17+
number of times until they get the entire word. As a bonus,
18+
keep track of the letters the player guessed and display a
19+
different message if the player tries to guess that letter
20+
again. Remember to stop the game when all the letters have
21+
been guessed correctly! Don’t worry about choosing a word
22+
randomly or keeping track of the number of guesses the player
23+
has remaining - we will deal with those in a future exercise.
24+
25+
An example interaction can look like this:
26+
27+
>>> Welcome to Hangman!
28+
_ _ _ _ _ _ _ _ _
29+
>>> Guess your letter: S
30+
Incorrect!
31+
>>> Guess your letter: E
32+
E _ _ _ _ _ _ _ E
33+
...
34+
And so on, until the player gets the word.
35+
'''
36+
import re
37+
38+
# Solution
39+
def modify_list(result, guess, answer):
40+
"""
41+
Print all the key in dict.
42+
43+
Arguments:
44+
result -- a list of the show pattern word.
45+
guess -- the letter of user's guess.
46+
answer -- the answer of word
47+
48+
Returns:
49+
result -- the list of word after modified.
50+
51+
"""
52+
guess = guess.lower()
53+
answer = answer.lower()
54+
if guess in answer:
55+
index_list = [x.start() for x in re.finditer(guess, answer)]
56+
for i in index_list:
57+
result[i] = guess.upper()
58+
else:
59+
print("Letter '{}' is not in the word".format(guess.upper()))
60+
print(' '.join(result))
61+
return result
62+
63+
def win(word_list):
64+
"""
65+
Check the user has guessed the right word or not.
66+
67+
Arguments:
68+
word_list -- the word list.
69+
70+
Returns:
71+
True/False -- return True if the word has been guessed right alse return false.
72+
"""
73+
if '_' not in word_list:
74+
return True
75+
else:
76+
return False
77+
78+
def main():
79+
right_answer = 'EVAPORATE'
80+
print('Welcome to Hangman!')
81+
guess_list = ['_' for i in range(len(right_answer))]
82+
print(' '.join(guess_list))
83+
while True:
84+
guess = input('Guess your letter: ')
85+
guess_list = modify_list(guess_list, guess, right_answer)
86+
if win(guess_list) == True:
87+
print('You win!')
88+
break
89+
90+
if __name__ == "__main__":
91+
main()
92+
93+
# >>> %Run test.py
94+
# Welcome to Hangman!
95+
# _ _ _ _ _ _ _ _ _
96+
# Guess your letter: e
97+
# E _ _ _ _ _ _ _ E
98+
# Guess your letter: a
99+
# E _ A _ _ _ A _ E
100+
# Guess your letter: w
101+
# Letter 'W' is not in the word
102+
# E _ A _ _ _ A _ E
103+
# Guess your letter: a
104+
# E _ A _ _ _ A _ E
105+
# Guess your letter: v
106+
# E V A _ _ _ A _ E
107+
# Guess your letter: q
108+
# Letter 'Q' is not in the word
109+
# E V A _ _ _ A _ E
110+
# Guess your letter: w
111+
# Letter 'W' is not in the word
112+
# E V A _ _ _ A _ E
113+
# Guess your letter: p
114+
# E V A P _ _ A _ E
115+
# Guess your letter: o
116+
# E V A P O _ A _ E
117+
# Guess your letter: r
118+
# E V A P O R A _ E
119+
# Guess your letter: t
120+
# E V A P O R A T E
121+
# You win!

0 commit comments

Comments
 (0)