【发布时间】:2013-11-03 16:36:14
【问题描述】:
在下面的代码中,我不断收到 NoneType 对象没有 len() 的响应,但代码中的任何地方都没有长度函数 - 有谁知道出了什么问题?
def constant_pension(salary, save, growth_rate, years):
if salary<0 or save<0 or save>100 or growth_rate<=-100 or years<=0: #invalid
return(None)
i=0
fund_list=[]
old_fund=0
new_fund=0
while i<years:
new_fund=old_fund*(1+growth_rate*.01)+salary*save*.01
fund_list.append(new_fund)
old_fund=new_fund
i=i+1
return(fund_list)
pass
【问题讨论】:
-
问题可能出在函数调用的位置。 (并在无效输入上引发异常!)
-
使用
len的任何地方。您能否显示它的调用位置以及错误的堆栈跟踪? -
如果您需要帮助查找错误,请向我们显示回溯。
-
给定的函数工作正常。请提供python遇到错误时给出的traceback。
-
请避免
while循环,因为它们可以很容易地被for循环替换。如果你想循环0(包括)和years(不包括)之间的所有数字,只需执行for i in range(years):。