Get GameWord
After every guess, we need to allow the user to understand the state of his/her guesses. We need to allow to see the number of right letters guessed already and the remaining letters that are needed to be guessed. To do that we create the get_guessed_word
function.
Scroll down to see the whole task code!
Compito
Swipe to start coding
result_list
is a list with the curent state of the gameword
: right letters and gaps.
- Set the
get_guessed_word
function using thegameword
and theletters_already_guessed
as arguments. - Set the
for
loop to work with thegameword
usingi
as an iterator andrange()
function. - Set condition to check if elements of the
gameword
are equal to theletters_already_guessed
. - Append the letter
list(gameword)[i])
to the theresult_list
if condition is satisfied, append'_ '
(with the space) otherwise. - Test the function using the
test_gameword
andtest_letters_already_guessed
lists.
Soluzione
99
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
# Set the get_guessed_word function
def get_guessed_word(gameword, letters_already_guessed):
result_list = []
# Set for loop to work with the gameword
for i in range(len(list(gameword))):
if list(gameword)[i] in set(letters_already_guessed):
# Append correct guessed letters to the result_list
result_list.append(list(gameword)[i])
else:
# Append '_ ' to the result_list
result_list.append('_ ')
# Appending the space to the result_list
return ''.join(result_list)
# Test the function
test_gameword = 'sunny'
test_letters_already_guessed = ['n']
print(get_guessed_word(test_gameword, test_letters_already_guessed))
Tutto è chiaro?
Grazie per i tuoi commenti!
Sezione 1. Capitolo 4
99
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
# Set the get_guessed_word function
___(gameword, ___):
result_list = []
# Set for loop to work with the gameword
___ i in ___(len(list(___))):
# Set the condition
if list(___)[i] in set(letters_already_guessed):
# Append correct guessed letters to the result_list
___(list(gameword)[i])
else:
# Append '_ ' to the result_list
___('_ ')
# Appending the space to the result_list
return ''.join(result_list)
# Test the function
test_gameword = 'sunny'
test_letters_already_guessed = ['n']
print(___(test_gameword, ___))
Chieda ad AI
Chieda pure quello che desidera o provi una delle domande suggerite per iniziare la nostra conversazione