在Python中计算总联邦税

时间:2018-10-28 21:56:34

标签: python

我正在编写一个程序,计算薪水,联邦税和营业税,然后根据薪水将其归类。之后,应该计算每个人的所有联邦税的总额,但是我的程序只是添加了最新的联邦税计算。并非每份薪水都征收联邦税。我只需要帮助弄清楚如何计算联邦税总额。 这是我的代码:

response = "yes"
over100 = 0
btwn50to100 = 0
btwn25to50 = 0
below25 = 0
while(response=="yes") or (response=="YES"):
    salary = input("Please one persons salary: ")
    if(salary>=100000):
        over100 = over100 + 1
        FederalTax = 0.20
    elif(salary>=50000) and (salary<100000):
        btwn50to100 = btwn50to100 + 1
        FederalTax = 0.15
    elif(salary>=25000) and (salary<50000):
        btwn25to50 = btwn25to50 + 1
        FederalTax = 0.15
    elif(salary<25000):
        below25 = below25 + 1
        FederalTax = 0.15
    StateTax = 0.05
    FederalTax = int(float(salary * FederalTax))
    StateTax = int(float(salary * StateTax))
    NetSalary = int(float(salary - FederalTax - StateTax))
    totalfederaltax = int(float(FederalTax + FederalTax))  #This is where I messed up
    print("Your federal tax is :" +str(FederalTax))
    print("Your state tax is :" +str(StateTax))
    print("Your net salary is: " +str(NetSalary))
    response = input("Would you like to continue?(yes/no): ")
print("*****")
print("The number of pepole who earned more than 100000 is: " +str(over100))
print("The number of pepole who earned More than or equal to 50000 and less than 100000 is: " +str(btwn50to100))
print("The number of pepole who earned More than or equal to 25000 and less than 50000 is: " +str(btwn25to50))
print("The number of pepole who earned Below 25000 is: " +str(below25))
print("The total federa tax is: " +str(totalfederaltax))

3 个答案:

答案 0 :(得分:0)

  

并非每份工资都征收联邦税。

IIUC,您需要从循环外的变量开始,然后在循环内添加(如果您希望将所有投入的总税额相加)

total_federal_tax = 0
while response.lower() =="yes":
    salary = int(input("Please one persons salary: ")) 
    # TODO: compute federal_tax
    total_federal_tax += salary * federal_tax
print(total_federal_tax) 

答案 1 :(得分:0)

import math


class Counter(object): # mutable object is needed to acces it from tuple
    def __init__(self, start_value=0):
        self.value = start_value

    def __str__(self):
        return str(self.value)


response = "yes"
over100 = Counter()
btwn50to100 = Counter()
btwn25to50 = Counter()
below25 = Counter()
totalFederalTax = 0

STATE_TAX_RATE = 0.05

TAX_MAP = {(0, 25000): (0.15, below25),
           (25000, 50000): (0.15, btwn25to50),
           (50000, 100000): (0.15, btwn50to100),
           (100000, math.inf): (0.20, over100)}


def calc_federal_tax(salary):
    for compartments, values in TAX_MAP.items():
        if compartments[0] <= salary < compartments[1]:
            values[1].value += 1
            return values[0]



while response.lower() == "yes":
    salary = input("Please one persons salary: ")
    federalTaxRate = calc_federal_tax(salary)

    federalTax = int(salary * federalTaxRate)
    stateTax  = int(salary * STATE_TAX_RATE)
    netSalary = int(salary - federalTax - stateTax)
    totalFederalTax += federalTax
    print("Your federal tax is :" + str(federalTax))
    print("Your state tax is :" + str(stateTax))
    print("Your net salary is: " + str(netSalary))
    response = input("Would you like to continue?(yes/no): ")
print("*****")
print("The number of pepole who earned more than 100000 is: " + str(over100))
print("The number of pepole who earned More than or equal to 50000 and less than 100000 is: " + str(btwn50to100))
print("The number of pepole who earned More than or equal to 25000 and less than 50000 is: " + str(btwn25to50))
print("The number of pepole who earned Below 25000 is: " + str(below25))
print("The total federal tax is: " + str(totalFederalTax))

答案 2 :(得分:-1)

这是一个有趣的程序。

response = "yes"
over100 = 0
btwn50to100 = 0
btwn25to50 = 0
below25 = 0
total_federal_tax = 0

while response.lower() == "yes":
    salary = int(input("Please one persons salary: "))
    if(salary >= 100000):
        over100 = over100 + 1
        FederalTax = 0.20
    elif(salary >= 50000) and (salary < 100000):
        btwn50to100 = btwn50to100 + 1
        FederalTax = 0.15
    elif(salary >= 25000) and (salary < 50000):
        btwn25to50 = btwn25to50 + 1
        FederalTax = 0.15
    elif(salary < 25000):
        below25 = below25 + 1
        FederalTax = 0.15

    StateTax = 0.05
    FederalTax = int(float(salary * FederalTax))
    StateTax = int(float(salary * StateTax))
    NetSalary = int(float(salary - FederalTax - StateTax))
    total_federal_tax += FederalTax
    print("Your federal tax is :" +str(FederalTax))
    print("Your state tax is :" +str(StateTax))
    print("Your net salary is: " +str(NetSalary))
    response = input("Would you like to continue?(yes/no): ")

print("*****")
print("The number of pepole who earned more than 100000 is: " +str(over100))
print("The number of pepole who earned More than or equal to 50000 and less than 100000 is: " +str(btwn50to100))
print("The number of pepole who earned More than or equal to 25000 and less than 50000 is: " +str(btwn25to50))
print("The number of pepole who earned Below 25000 is: " + str(below25))
print("The total federa tax is: " + str(total_federal_tax))