Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
while loop problem
#1
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.

[Image: tB7wgTF]

Thanks in Advance,

Fid
Reply
#2
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
Reply
#3
Thank You @jefsummers. The problem has been solved.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  for loop problem with opencv Miradox 1 2,390 Dec-23-2018, 10:39 PM
Last Post: stullis
  problem on for-if loop alrm31015 1 2,147 Aug-21-2018, 05:48 PM
Last Post: Gribouillis

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020