Hints 1/2
It is difficult guessing the word from the such a big file. So, we allow the user to use the hint. We will implement the hint part of the program in 2 steps. The hints_match
function defines if the current state of gameword
(correct guessed letters and gaps) match the exact word from the file. If it does, then function returns True, False otherwise.
Oppgave
Swipe to start coding
- Set the
hints_match
function using theword_to_match
and theword_from_list
as arguments. - Delete spaces in the
word_to_match
. - Compare lengths of the
test_list
andother_list
. - If lengths aren't the same, return
False
. - Set
for
loop to work with thetest_list
. - Increase the
counter
. - Test the function using the
test_word_to_match
andtest_word_from_list
lists.
Løsning
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
# Set the hints_match function
def hints_match(word_to_match, word_from_list):
# Delete spaces in the word_to_match
test_list = list(word_to_match.replace(' ', ''))
other_list = list(word_from_list)
# Compare lengths of the test_list and the other_list
if len(test_list) != len(other_list):
# Return False
return False
else:
counter = 0
# Set for loop to work with the test_list
for i in range(len(test_list)):
if test_list[i] == '_':
if other_list[i] not in test_list:
# Increase the counter
counter += 1
elif test_list[i] == other_list[i]:
# Increase the counter
counter += 1
if counter == len(word_to_match.replace(' ', '')):
return True
else:
return False
# Test the function
test_word_to_match = 's_ _ _ _'
test_word_from_list = 'sunny'
print(hints_match(test_word_to_match, test_word_from_list))
Alt var klart?
Takk for tilbakemeldingene dine!
Seksjon 2. Kapittel 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
# Set the hints_match function
___(word_to_match, ___):
# Delete spaces in the word_to_match
test_list = list(___.replace(' ', ''))
other_list = list(word_from_list)
# Compare lengths of the test_list and the other_list
if len(___) != len(other_list):
# Return False
___
else:
counter = 0
# Set for loop to work with the test_list
___ i in range(___(___)):
if test_list[i] == '_':
if other_list[i] not in test_list:
# Increase the counter
___
elif test_list[i] == other_list[i]:
# Increase the counter
___
if counter == len(word_to_match.replace(' ', '')):
return True
else:
return False
# Test the function
test_word_to_match = 's_ _ _ _'
test_word_from_list = 'sunny'
print(___(___, ___))
Spør AI
Spør om hva du vil, eller prøv ett av de foreslåtte spørsmålene for å starte chatten vår