+ 1

Solution for IndexError: list assignment index out of range😭

This is the code ---------------------------------------------------------------------- def countApplesAndOranges(s, t, a, b, apples, oranges): ap=[] oran=[] for i in range(len(apples)) : ap[i]=apples[i]+a for i in range(len(oranges)) : oran[i]=oranges[i]+b p=0 og=0 for i in range(len(apples)-1) : if (ap[i]>s and ap[i]<s): p+=1 for i in range(len(oranges)-1): if (oran[i]>s and oran[i]<t): og+=1 print (p) print (og) ---------------------------------------------------------------------- The error I am facing is this: ---------------------------------------------------------------------- Traceback (most recent call last) File "Solution.py", line 52, in <module> countApplesAndOranges(s, t, a, b, apples, oranges) File "Solution.py", line 14, in countApplesAndOranges ap[i]=apples[i]+a IndexError: list assignment index out of range ---------------------------------------------------------------------- Thanks for any possible help out there

20th Dec 2020, 2:19 AM
Senithu Sethmin👿
Senithu Sethmin👿 - avatar
2 odpowiedzi
+ 5
Senithu Sethmin👿 write len(apples) - 1 for i in range(len(apples) - 1): ap[i] = apples[i] + a Same for oranges As array index start from 0 so you should always do listsize - 1
20th Dec 2020, 2:51 AM
A͢J
A͢J - avatar
+ 2
Thanks
20th Dec 2020, 8:30 AM
Senithu Sethmin👿
Senithu Sethmin👿 - avatar