在这个问题上,它表示前10,000个税收没有征税,接下来的20,000个税收10%,接下来的40,000个税率20%,而前70,000个税率30%.我对python很新,但这是我到目前为止所做的.我不确定我哪里出错了.我认为这是因为我没有定义"税"变量,但我不确定.任何帮助将非常感激.谢谢!
**如果用户输入负数并且我不确定如何将其添加到我的for循环中,则代码也必须终止.
def income(drach):
for drach in range(10000):
tax = 0
for drach in range(10000 , 30000):
tax = ((drach - 10000)*0.1)
for drach in range(30000 , 70000):
tax = ((drach - 30000)*0.2) + 2000
for drach in range(70000 , 10**999):
tax = ((drach - 70000)*0.3) + 10000
print tax
Run Code Online (Sandbox Code Playgroud)
我认为这是正确的税收模式:
def tax(income):
tax=0;
if income > 70000 :
tax += (income-70000)* 0.3
income = 70000
if income > 30000 :
tax += (income-30000)* 0.2
income = 30000
if income > 10000 :
tax += (income-10000)* 0.1
return tax;
Run Code Online (Sandbox Code Playgroud)
关于负数位 -
if drach < 0:
break
Run Code Online (Sandbox Code Playgroud)
当 drach 为负数时将终止 for 循环。有关此内容的更多信息,请参阅有关Python 流控制工具的更多信息。当您收到用户输入时,您应该这样做。当您计算用户税时则不然。
关于计算税收,您不会希望在drachs. 这比您需要的计算量要多得多。
您需要计算所谓的累进税。
这是我用于计算累进税的数据结构和相关函数。编辑-我的功能犯了一些错误。现在应该是正确的。
tax_bracket = (
#(from, to, percentage)
(0, 10000, None),
(10000, 30000, .10),
(30000, 70000, .20),
(70000, None, .30),
)
def calc_tax(drach):
tax = 0
for bracket in tax_bracket:
# If there is no tax rate for this bracket, move on
if not bracket[2]:
continue
# Check if we belong in this bracket
if drach < bracket[0]:
break
# Last bracket
if not bracket[1]:
tax += ( drach - bracket[0] ) * bracket[2]
break
if drach >= bracket[0] and drach >= bracket[1]:
tax += ( bracket[1] - bracket[0] ) * bracket[2]
elif drach >= bracket[0] and drach <= bracket[1]:
tax += ( drach - bracket[0] ) * bracket[2]
else:
print "Error"
return tax
income = 50000
tax = calc_tax(income)
print tax
Run Code Online (Sandbox Code Playgroud)