Reputation: 37
I created a function to compare the characters in my_word and other_word. my_word is a string with '_' characters, other_word is a string with regular English word.
The function returns: boolean, True if all the actual letters of my_word match the corresponding letters of other_word, or the letter is the special symbol '_' , and my_word and other_word are of the same length;False otherwise.
Hint:, the letter (_ ) cannot be one of the letters in the other_word that has already been revealed. So if run match_with_gaps("a_ ple", "apple") ,it returns False.
Here is the code:
`def match_with_gaps(my_word, other_word):
my_word=my_word.replace(' ','')
if len(my_word)==len(other_word):
for char1 in my_word:
if char1 not in other_word and char1 != '_':
return False
my_list=list(my_word)
other_list=list(other_word)
for w in my_list:
if other_list.count(w)>1:
return False
return True
else: return False`
Run the code. It returns SyntaxError:invalid character in identifier
with the line: if other_list.count(w)>1. But I think it is acceptable to use the list.count() function. What's wrong with it?
Upvotes: 2
Views: 812
Reputation: 5479
I copied your code and cleaned it up. The colon after 1 was not recognized as a colon. You also had a ' character at the start and end of your code. This works fine now:
def match_with_gaps(my_word, other_word):
my_word=my_word.replace(' ','')
if len(my_word)==len(other_word):
for char1 in my_word:
if char1 not in other_word and char1 != '_':
return False
my_list=list(my_word)
other_list=list(other_word)
for w in my_list:
if other_list.count(w)>1:
return False
return True
else:
return False
Upvotes: 1