while loop problem - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: Data Science (https://python-forum.io/forum-44.html) +--- Thread: while loop problem (/thread-27746.html) |
while loop problem - fid - Jun-19-2020 Hi Guys, I am having a problem with a while loop for a tax calculator. You don't have to understand taxes to understand the problem. Here is the Code. I think that the problem is the lst isnt appending. But there may be more issues that I dont see. class marginal_calc: def __init__(self,value): self.value = value def calculate(self): bracket = [0,12500,50000,150000] percentage = [0,0.2,0.4,0.45] bracket.append(self.value) bracket = [num for num in bracket if num <= self.value] bracket = list(set(bracket)) bracket.sort() p2 = [] count = 0 while count < (len(bracket)-1): p2.append(percentage[count]) count+=1 lst = [] index = 0 while index < len(bracket)-1: a = index if index > 0: b = (bracket[a]-bracket[a-1] )* p2[a-1] lst.append(b) index = index + 1 return('total',sum(lst)) print(marginal_calc(30000).calculate())Here is an image explainig how the code should work. Thanks in Advance, Fid RE: while loop problem - jefsummers - Jun-19-2020 In lines 31-40, the value of b is calculated one time (I put in print statements to test) and has the value of zero. lst then has the value of [0] and sum of lst is then obviously 0. Problem is in the logic of that loop RE: while loop problem - fid - Jun-19-2020 Thank You @jefsummers. The problem has been solved. |