【发布时间】:2019-10-17 05:27:21
【问题描述】:
所以我有这段代码:
def effective_rate(db : {str: {(int,int) : float}}, state : str, income : int ) -> float:
tax = 0
for state in db.values():
for(lower_limit, higher_limit), tax_rate in state.items():
if (income >= lower_limit):
tax = tax + (tax_rate*(higher_limit - lower_limit))
if (income <= higher_limit):
tax = tax + (tax_rate*(income-lower_limit))
return tax
这是检查我的代码:
db1 = {'CT': {( 0, 12_499): .02,
( 12_500, 49_999): .04,
( 50_000, None): .06},
'IN': {(0, None): .04},
'LA': {( 0, 9_999): .03,
( 10_000, 12_499): .05,
( 12_500, 49_999): .055,
( 50_000, 299_999): .06,
(300_000, None): .078},
'MA': {(0, None): .055}}
answer = effective_rate(db1,'CT',40_000)
我遇到的问题是我的结果应该打印0.0337495
另一个问题是如果higher_limit 是None,它会引发TypeError: unsupported operand type(s) for -: 'NoneType' and 'int'
我将如何使它为 if 语句增加不止一次并且正确?以及如何阻止错误引发?
【问题讨论】:
-
你为什么不把
None改成一个非常高的值,比如99999999999?顺便说一句:“12_500”怎么会是整数? -
@Aryerez 从 3.6 开始我们可以这样写整数:python.org/dev/peps/pep-0515
标签: python if-statement nested increment