python - 在 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))
解决方案
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))
推荐阅读
- javascript - 使用Rails Simple Form gem时如何通过JS onclick更改输入字段值
- jakarta-ee - 使用`@DataSourceDefinition`(或@MailSessionDefinition)时是否可以隐藏密码
- c# - 使 c# Windows 窗体工具公开
- python - python中列表组合的问题
- json - 尝试使用 ARM 模板在 azure 中部署应用程序网关,但在创建时无法引用侦听器
- apache-kafka - Kafka 分区再平衡
- sql - 从多个表中选择 DISTINCT 行,其中只有 1 列具有不同的记录
- python - Azure Runbooks - 解析 Python 参数
- python - curl 和 python cookie 是不同的
- css - Atom 编辑器突出显示问题