Is not a Letter
What if the user input &
or @
? We need to 'punish' the user by decreasing number of warnings (or guesses if there are no warnings left). Only one symbol !
means that the user is asking for the hint.
We need to take into account these cases.
Task
Swipe to start coding
- Set condition if the input is
'!'
. - Print possible matches using the
show_possible_matches
function and theget_guessed_word(gameword, used_letters)
as an argument. - Decrease the
warnings_remaining
by one. - Set condition if there are less than 0 (not included) warnings.
- Decrease the
guesses_reamaining
by one.
Solution
99
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
def hangman(gameword):
used_letters = []
guesses_remaining, warnings_remaining = 6, 3
print('A gameword is', len(gameword), 'letters long')
while True:
if not is_word_guessed(gameword, used_letters):
print('You have', int(guesses_remaining), 'guess(es) left and', int(warnings_remaining), 'warning(s) left!' + 'Available letters: ',get_available_letters(used_letters) + 'Game word: ', get_guessed_word(gameword, used_letters))
guess = str.lower(input('Please, input a letter: '))
if not guess.isalpha():
# Condition: the guess is '!'
if guess == '!':
# Print possible matches
print('Possible word matches are: ',
show_possible_matches(get_guessed_word(gameword, used_letters)))
else:
print('That is not a valid symbol.')
# Decrease the warnings_remaining
warnings_remaining -= 1
# Condition: the warnings_remaining is lower than 0
if warnings_remaining < 0:
print('You have no warnings left so you lose 1 guess!')
# Decrease the guesses_remaining
guesses_remaining -= 1
else:
print('You loose 1 warning!')
elif guess in set(used_letters):
pass
elif guess in set(gameword):
pass
else:
pass
Everything was clear?
Thanks for your feedback!
Section 3. Chapter 2
99
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
def hangman(gameword):
used_letters = []
guesses_remaining, warnings_remaining = 6, 3
print('A gameword is', len(gameword), 'letters long')
while True:
if not is_word_guessed(gameword, used_letters):
print('You have', int(guesses_remaining), 'guess(es) left and', int(warnings_remaining), 'warning(s) left!' + 'Available letters: ',get_available_letters(used_letters) + 'Game word: ', get_guessed_word(gameword, used_letters))
guess = str.lower(input('Please, input a letter: '))
if not guess.isalpha():
# Condition: the guess is '!'
if guess == ___:
# Print possible matches
print('Possible word matches are: ', ___(get_guessed_word(___)))
else:
print('That is not a valid symbol.')
# Decrease the warnings_remaining
___ -= ___
# Condition: the warnings_remaining is lower than 0
if warnings_remaining ___:
print('You have no warnings left so you lose 1 guess!')
# Decrease the guesses_remaining
___
else:
print('You loose 1 warning!')
elif guess in set(used_letters):
pass
elif guess in set(gameword):
pass
else:
pass
Ask AI
Ask anything or try one of the suggested questions to begin our chat