Hints 2/2
The function we are going to implement doesn't work without the function
from the previous step. The hints_match
function has been already implemeneted in the code.
The second hint function is named show_possible_matches
, and shows the word from the file (word_from_list
) if this word matches the current state of the gameword
.
Uppgift
Swipe to start coding
- Set the
show_possible_matches
function using theword_to_match
as an argument. - Set
for
loop to work with the elements of thetest_word_list
. - If the
hints_match
isTrue
, append an element to thepossible_matches
list. - Set condition if the
possible_matches
list is empty. - Test the function using the
test_word_to_match
list.
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
30
31
32
33
34
35
36
# The word_list to test
test_word_list = ['sun', 'sky', 'water', 'fire', 'nature']
# We have implemented this function at the last step
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
# Set the show_possible_matches function
def show_possible_matches(word_to_match):
possible_matches=[]
# Set for loop to work with the test_word_list
for word in test_word_list:
if hints_match(word_to_match, word):
# Append an element to the possible matches list_
possible_matches.append(word)
# Set condition if the possible_matches list is empty
if len(possible_matches) == 0:
return 'No matches found'
else:
return ' '.join(possible_matches)
Var allt tydligt?
Tack för dina kommentarer!
Avsnitt 2. Kapitel 3
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
# The word_list to test
test_word_list = ['sun', 'sky', 'water', 'fire', 'nature']
# Set the show_possible_matches function
___:
possible_matches=[]
# Set for loop to work with the test_word_list
___ word in ___:
if hints_match(word_to_match, word):
# Append an element to the possible matches list_
___.___(word)
# Set condition if the possible_matches list is empty
if len(possible_matches) == ___:
return 'No matches found'
else:
return ' '.join(possible_matches)
# We have implemented this function at the last step
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
Fråga AI
Fråga vad du vill eller prova någon av de föreslagna frågorna för att starta vårt samtal