Help! Error - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: General Coding Help (https://python-forum.io/forum-8.html) +--- Thread: Help! Error (/thread-27013.html) |
Help! Error - bwdu - May-22-2020 Hİ everyone! tosumup is a huge list so i'll only paste the begining of it tosumup = [[5, 5, 5, 3, 3, 3, 5, 0, 0, 0, 0, 1, 0, 3, 5, 3, 0, 0, 5, 5, 0, 3, 5, 0, 0, 5, 0, 0, 0, 0, 1, 0, 5, 0, 1, 5, 5, 5, 5, 0, 0, 0, 3, 0, 0, 0, 0, 0, 0, 0, 5, 0, 0, 0, 0, 3, 0, 0, 0, 0, 5, 0, 0, 5, 5, 0, 0, 0, 0, 0, 0, 5, 0, 0, 5, 3, 0, -5, 5, 0, 0, 0, 0, 3, 0, 5, 0], [0, 5, -5, 3, 0, 3, 3, 0, 0, 0, -3, 1, 0, 0, 0, 3, 0, 3, 1, 0, 0, 0, 0, 0, 0, 0, 0, 3, 5, 0, 0, 0, 0, 0, -3, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 3, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 3, 0, 0, 5, 0, 0, -5, 3, 0, 0, 0, 0, 1, 0, 5, 0], [], []] zeros = [51, 67, 0, 0, 81, 0, 73, 66, 83, 47, 66, 60, 0, 0, 0, 82, 61, 74, 48, 71, 0, 84, 84, 62, 82, 71, 0, 64, 72, 86, 34, 37, 73, 0, 87, 53, 83, 63, 43, 63, 77, 0, 24, 44, 78, 83, 70, 0, 78, 41, 0, 81, 64, 82, 62] tosumup has empty lists and zeros has 0's so, to prevent this error: I wrote this line but it didn't worked. Can you help me with that? if tosumup[i] != [] and zeros[i] != 0: This is my code snippet; avareges = [] sums = [] for i in range(len(tosumup)): s = sum(tosumup[i]) sums.append(s) if tosumup[i] != [] and zeros[i] != 0: a = sums[i]/(len(tosumup[i])- (zeros[i])) avareges.append(a) print(avareges) print(sums) RE: Help! Error - Yoriz - May-22-2020 You could wrap you code in a try/except block that checks for ZeroDivisionError and then decide what you want to happen when it occurs see Handling Exceptions RE: Help! Error - Larz60+ - May-22-2020 Please, always post entire, unaltered error traceback. It contains valuable information. RE: Help! Error - bwdu - May-22-2020 (May-22-2020, 12:27 PM)Larz60+ Wrote: Please, always post entire, unaltered error traceback.
RE: Help! Error - ibreeden - May-22-2020 What if tosumup[i] = [0, 0, 0] ? Then zeros[i] would be 3 i suppose (you don't mention how it is filled). And then the if statement would execute the next line. Which would raise the error you got.So it is best to always test exactly what you need to check, namely: (len(tosumup[i]) - (zeros[i])) must not be 0. if (len(tosumup[i]) - (zeros[i])) != 0: a = sums[i]/(len(tosumup[i]) - (zeros[i])) else a = None # I think you need to match the indexes later, so you should add someting. avareges.append(a) RE: Help! Error - deanhystad - May-22-2020 count = len(tosumup[i]) - zeros[i] a = s if count < 1 else s / count |