Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Aprenda Hints 1/2 | Crafting a Classic Hangman Game
Crafting a Classic Hangman Game

book
Hints 1/2

Guessing the word from a large file can be challenging, so we provide users with the option to use a hint.

Adding Hint Functionality

To integrate hint functionality into our program, we'll proceed in two phases. The hints_match function determines whether the current state of the gameword—which includes correctly guessed letters and placeholders—corresponds exactly to any word in the file. The function will return True if there's a match and False otherwise.

Tarefa

Swipe to start coding

  1. Define the hints_match function with word_to_match and word_from_list as parameters.
  2. Remove spaces from word_to_match.
  3. Compare the lengths of test_list and other_list.
  4. Return False if the lengths differ.
  5. Initialize a for loop to iterate through test_list.
  6. Increment the counter.

Solução

def hints_match(word_to_match, word_from_list):
test_list = list(word_to_match.replace(' ', ''))
other_list = list(word_from_list)
if len(test_list) != len(other_list):
return False
else:
counter = 0
for i in range(len(test_list)):
if test_list[i] == '_':
if other_list[i] not in test_list:
counter += 1
elif test_list[i] == other_list[i]:
counter += 1
if counter == len(word_to_match.replace(' ', '')):
return True
else:
return False

Mark tasks as Completed
Tudo estava claro?

Como podemos melhorá-lo?

Obrigado pelo seu feedback!

Seção 1. Capítulo 6
AVAILABLE TO ULTIMATE ONLY
We use cookies to make your experience better!
some-alt