-
Notifications
You must be signed in to change notification settings - Fork 1
/
hangmanproject_unit7.py
81 lines (68 loc) · 3.07 KB
/
hangmanproject_unit7.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
# hangman project - unit seven part 1 and 2 exercise.
# author - lirom mizrahi
'''
7.3.1
In this exercise you will show the player his progress in guessing the secret word.
Write a function called show_hidden_word defined as follows:
def show_hidden_word(secret_word, old_letters_guessed):
The function's acceptance values
A string called secret_word. The string represents the secret word that the player has to guess.
A list called old_letters_guessed. The list contains the letters the player has guessed so far.
The return values of the function
The function returns a string consisting of letters and underscores. The string shows the
letters from the old_letters_guessed list that are in the secret_word string in their
appropriate position, and the other letters in the string (which the player has not yet guessed)
as underlines.
Example of running the show_hidden_word function:
>>> secret_word = "mammals"
>>> old_letters_guessed = ['s', 'p', 'j', 'i', 'm', 'k']
>>> print(show_hidden_word(secret_word, old_letters_guessed))
m _ m m _ _ s
Guidelines
To make it clear to the player how many letters are left to guess, space the string
when you print the underscores
7.3.2
Time to check if the player has already won, right?
In this exercise you will write a function that checks whether the player was able to guess the secret word and thus won the game!
Write a function called check_win defined as follows:
def check_win(secret_word, old_letters_guessed):
The function's acceptance values
A string called secret_word. The string represents the secret word that the player has to guess.
A list called old_letters_guessed. The list contains the letters the player has guessed so far.
The return values of the function
The function returns true if all the letters that make up the secret word are included in the list of letters that the user guessed. Otherwise, the function returns false.
Examples of running the check_win function
>>> secret_word = "friends"
>>> old_letters_guessed = ['m', 'p', 'j', 'i', 's', 'k']
>>> print(check_win(secret_word, old_letters_guessed))
False
>>> secret_word = "yes"
>>> old_letters_guessed = ['d', 'g', 'e', 'i', 's', 'k', 'y']
>>> print(check_win(secret_word, old_letters_guessed))
True
'''
def show_hidden_word(secret_word, old_letters_guessed):
result = ''
for letter in secret_word:
if letter in old_letters_guessed:
result += letter + ' '
else:
result += '_ '
return result.strip()
def check_win(secret_word, old_letters_guessed):
for letter in secret_word:
if letter not in old_letters_guessed:
return False
return True
def main():
secret_word = "lironrrrg"
old_letters_guessed = ['s', 'p', 'j', 'i', 'l', 'r']
print(show_hidden_word(secret_word, old_letters_guessed))
secret_word = "friends"
old_letters_guessed = ['m', 'p', 'j', 'i', 's', 'k']
print(check_win(secret_word, old_letters_guessed))
secret_word = "yes"
old_letters_guessed = ['d', 'g', 'e', 'i', 's', 'k', 'y']
print(check_win(secret_word, old_letters_guessed))
if __name__ == "__main__":
main()