问题描述
这是我的代码,我已使计算器的联邦税部分起作用,但我似乎无法使计算器的州税部分起作用。我不断收到错误消息。有谁知道如何解决这个问题。我是初学者:)
def come():
while True:
try:
print("Error: come `")
continue
else:
break
return i
def tax(income):
if income <= 0000:
tax = ome * 0.10
elif income <= 0000:
tax = (ome - .5) * 0.12
come = come()
tax = tax(0000)
0000
0000
if __name__ == "__main__":
inco
解决方法
你的程序有一些逻辑和语法错误(一些早期的帖子已经指出)。这是一个工作版本,可能有助于您制定最终版本。 (我故意尽量减少更改,以便您可以继续自己工作)
def read_income():
while True:
try:
income = int(input("Enter your gross income: "))
state = input("What state do you reside in: ") # string
except ValueError:
print("Error: Income format incorrect enter <number only>")
continue
else:
break
return income,state
def calculate_tax(income):
income_bracket = [0.10,0.12,0.22,0.24,0.32,0.35,0.37]
# 0 1 2 3 4 5 6
#deductibles = [98.5,4617.5,14605.5] # this should be better DS.
if income <= 9875:
tax = income * income_bracket[0]
elif 9876 < income <= 40125:
tax = (income - 98.5) * income_bracket[1]
elif income <= 85525:
tax = (income - 4617.5) * income_bracket[2]
elif income <= 163300:
tax = (income - 14605.5) * income_bracket[3]
elif income <= 207350:
tax = (income - 33271.5) * income_bracket[4]
elif income <= 518400:
tax = (income - 47367.5) * income_bracket[5]
elif income >= 518401:
tax = (income - 156235) * income_bracket[6]
return tax
if __name__ == "__main__":
income,state = read_income()
tax = calculate_tax(income)
print('You payed an amount of',tax,'in tax','you are left with',income - tax)
stateTax = {"alabama": .04,"alaska": 1,"arizona": .035,"arkansas": .06,"california": .09,"colorado": .0463,"connecticut": .05,"delaware": 0.066,"florida": 1,"georgia": 0.0575,"hawaii": .08,"idaho": 0.06625,"illinois": 0.0495,"indiana": 0.0323,"iowa": .04,"kansas": .0435,}
#for state,rate in stateTax.items():
# print(state,rate)
输出:
Enter your gross income: 45000
What state do you reside in: iowa
You payed an amount of 8884.15 in tax you are left with 36115.85
>>>