+ 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 :<

15th Jul 2021, 8:47 PM
Ailana
Ailana - avatar
34 Answers
+ 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
15th Jul 2021, 8:53 PM
Slick
Slick - avatar
+ 1
please post the actual error message or copy the code into the playgound, save and share here
15th Jul 2021, 8:55 PM
Slick
Slick - avatar
+ 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'
15th Jul 2021, 8:56 PM
Ailana
Ailana - avatar
+ 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.
15th Jul 2021, 8:58 PM
Slick
Slick - avatar
+ 1
Calvin Thomas i usually use pyperclip, doesnt seem to work on mobile though
16th Jul 2021, 12:10 PM
Slick
Slick - avatar
+ 1
Slick I see.
16th Jul 2021, 12:16 PM
Calvin Thomas
Calvin Thomas - avatar
+ 1
Ahmed Hassan what a sh... isTHAT???
17th Jul 2021, 5:14 PM
Oma Falk
Oma Falk - avatar
0
I fixed it but I still get on testcase wrong :<
15th Jul 2021, 8:54 PM
Ailana
Ailana - avatar
0
Test case 3 = pineapple should equal apple Terms = appl
15th Jul 2021, 8:55 PM
Ailana
Ailana - avatar
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
15th Jul 2021, 9:11 PM
Ailana
Ailana - avatar
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.
15th Jul 2021, 9:14 PM
Slick
Slick - avatar
0
wdym printing that value? Like this? : print(len(self.words[i+1])
15th Jul 2021, 10:24 PM
Ailana
Ailana - avatar
0
try printing i+1 in each loop, to see what the index value you plug in will be
15th Jul 2021, 10:26 PM
Slick
Slick - avatar
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 ?
15th Jul 2021, 10:33 PM
Ailana
Ailana - avatar
0
its on line 13, if i+1 gives an index error, try it without the +1. indexing starts at 0
15th Jul 2021, 10:37 PM
Slick
Slick - avatar
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]
15th Jul 2021, 10:39 PM
Ailana
Ailana - avatar
0
yes, except it would aslo be in the return statement. try and take out the +1's
15th Jul 2021, 10:40 PM
Slick
Slick - avatar
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
15th Jul 2021, 10:42 PM
Ailana
Ailana - avatar
0
self.words+1 isn't a statement. I think you're trying to add a number and a list as the error says
15th Jul 2021, 10:43 PM
Slick
Slick - avatar
0
Isnt that what u said? Though
15th Jul 2021, 10:54 PM
Ailana
Ailana - avatar