Python returns 递归调用后什么都没有
Python returns nothing after recursive call
我正在开发一个 python 脚本,该脚本可以根据个人的收入计算个人的税收。
税收制度要求人们根据他们的富裕程度或收入来征税。
前1000不征税,
接下来的 9000 被征税 10%
接下来的 10200 征税 15%
接下来的 10550 被征税 20%
下一个 19250 被征税 25%
在上述之后剩下的任何东西都按 30%
征税
我有代码 运行 并且正在运行,我能够使用递归使代码运行以遵循上述条件。
但是,我在return输入 total_tax 时遇到问题,它应该是函数的 return 值。
例如,20500的收入应纳税2490.0。
下面是我的代码片段:
def get_tax(income, current_level=0, total_tax=0,):
level = [0, 0.1, 0.15, 0.2, 0.25, 0.3]
amount = [1000, 9000, 10200, 10550, 19250, income]
if income > 0 and current_level <=5:
if income < amount[current_level]:
this_tax = ( income * level[current_level] )
income -= income
else:
this_tax = ( level[current_level] * amount[current_level] )
income -= amount[current_level]
current_level += 1
total_tax += this_tax
print total_tax
get_tax(income, current_level, total_tax)
else:
final = total_tax
return final
get_tax(20500)
正如您从代码片段中看到的,当我将 return 语句放在 else 块中时它不起作用,我也尝试过在没有 else 块的情况下这样做,但它仍然不起作用。
这是 Repl.it
上的片段 link
它没有返回任何内容,因为你没有 return
ing。
return get_tax(income, current_level, total_tax)
.
现在它正在返回一些东西,您需要对返回值做一些事情。
我正在开发一个 python 脚本,该脚本可以根据个人的收入计算个人的税收。
税收制度要求人们根据他们的富裕程度或收入来征税。
前1000不征税,
接下来的 9000 被征税 10%
接下来的 10200 征税 15%
接下来的 10550 被征税 20%
下一个 19250 被征税 25%
在上述之后剩下的任何东西都按 30%
我有代码 运行 并且正在运行,我能够使用递归使代码运行以遵循上述条件。
但是,我在return输入 total_tax 时遇到问题,它应该是函数的 return 值。
例如,20500的收入应纳税2490.0。
下面是我的代码片段:
def get_tax(income, current_level=0, total_tax=0,):
level = [0, 0.1, 0.15, 0.2, 0.25, 0.3]
amount = [1000, 9000, 10200, 10550, 19250, income]
if income > 0 and current_level <=5:
if income < amount[current_level]:
this_tax = ( income * level[current_level] )
income -= income
else:
this_tax = ( level[current_level] * amount[current_level] )
income -= amount[current_level]
current_level += 1
total_tax += this_tax
print total_tax
get_tax(income, current_level, total_tax)
else:
final = total_tax
return final
get_tax(20500)
正如您从代码片段中看到的,当我将 return 语句放在 else 块中时它不起作用,我也尝试过在没有 else 块的情况下这样做,但它仍然不起作用。
这是 Repl.it
上的片段 link它没有返回任何内容,因为你没有 return
ing。
return get_tax(income, current_level, total_tax)
.
现在它正在返回一些东西,您需要对返回值做一些事情。