Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Impara Get GameWord | Game Word
Beginner Final Project: Hangman

book
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.

  1. Set the get_guessed_word function using the gameword and the letters_already_guessed as arguments.
  2. Set the for loop to work with the gameword using i as an iterator and range() function.
  3. Set condition to check if elements of the gameword are equal to the letters_already_guessed.
  4. Append the letter list(gameword)[i]) to the the result_list if condition is satisfied, append '_ '(with the space) otherwise.
  5. Test the function using the test_gameword and test_letters_already_guessed lists.

Soluzione

# 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?

Come possiamo migliorarlo?

Grazie per i tuoi commenti!

Sezione 1. Capitolo 4
# 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

expand
ChatGPT

Chieda pure quello che desidera o provi una delle domande suggerite per iniziare la nostra conversazione

some-alt