+ 4
Syntax Error fix ?
Task : Guessing a misspelled word into a corrected spelled word (in a list) Words is a list of correct words and terms are the misspelled words class Dictionary: def __init__(self, words): self.words = words def find_most_similar(self, term): self.term = term for i in range(len(self.words)): if self.term[1:4] in self.words[i] and len(self.term) < len(self.words[i]): return self.words[i] elif self.term[1:4] in self.words[i+1] and len(self.term) < len(self.words[i+1]: return self.words[i+1] why is there a syntax error! please explain :<
34 Réponses
+ 4
Right off the bat, i can see in your final elif statement that you forgot closing parenthesis.
The exact error message would be very helpful
+ 1
please post the actual error message or copy the code into the playgound, save and share here
+ 1
class Dictionary:
def __init__(self, words):
self.words = words
def find_most_similar(self, term):
self.term = term
for i in range(len(self.words)):
if self.term[1:4] in self.words[i] and len(self.term) < len(self.words[i]):
return self.words[i]
elif self.term[1:4] in self.words[i+1] and len(self.term) < len(self.words[i+1]):
return self.words[i+1]
error = 'pineapple' should equal 'apple'
+ 1
if its a syntax error, there will be an error message when you run the code. Copy and paste that please.
if it works, but just doesnt have expected output, than thats a logic error in your program.
+ 1
Calvin Thomas i usually use pyperclip, doesnt seem to work on mobile though
+ 1
Slick I see.
+ 1
Ahmed Hassan
what a sh... isTHAT???
0
I fixed it but I still get on testcase wrong :<
0
Test case 3 = pineapple should equal apple
Terms = appl
0
Traceback (most recent call last):
File "tests.py", line 13, in <module>
test.assert_equals(test_dict.find_most_similar('coddwars'),'codewars')
File "/workspace/default/solution.py", line 11, in find_most_similar
elif self.term[1:4] in self.words[i+1] and len(self.term) < len(self.words[i+1]):
IndexError: list index out of range
0
Thanks! its an Index error it says, so whatever number was put into one of those index brackets [ ] is over the limit.
try printing that value. you may also be causght in the common, "off by one" problem.
0
wdym printing that value?
Like this? : print(len(self.words[i+1])
0
try printing i+1 in each loop, to see what the index value you plug in will be
0
class Dictionary:
def __init__(self,words):
self.words = words
def find_most_similar(self,term):
self.term = term
for i in range(len(self.words)+1):
if self.term[1:4] in self.words[i] and len(self.term) < len(self.words[i]):
return self.words[i]
elif self.term[1:4] in self.words[i+1] and len(self.term) < len(self.words[i+1]):
return self.words[i+1]
like this ?
0
its on line 13, if i+1 gives an index error, try it without the +1.
indexing starts at 0
0
like this
class Dictionary:
def __init__(self,words):
self.words = words
def find_most_similar(self,term):
self.term = term
for i in range(len(self.words+1)):
if self.term[1:4] in self.words[i] and len(self.term) < len(self.words[i]):
return self.words[i]
elif self.term[1:4] in self.words[i+1] and len(self.term) < len(self.words[i]):
return self.words[i+1]
0
yes, except it would aslo be in the return statement. try and take out the +1's
0
Traceback (most recent call last):
File "tests.py", line 5, in <module>
test.assert_equals(test_dict.find_most_similar('strawbery'),'strawberry')
File "/workspace/default/solution.py", line 8, in find_most_similar
for i in range(len(self.words+1)):
TypeError: can only concatenate list (not "int") to list
0
self.words+1 isn't a statement. I think you're trying to add a number and a list as the error says
0
Isnt that what u said? Though