计算联邦税总额

2024-09-20 05:41:11 发布

您现在位置: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))

Tags: andtheisresponseprint联邦thansalary
3条回答
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))

这是一个有趣的节目。你知道吗

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))

Not all the federal taxes per salary.

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) 

相关问题 更多 >