【问题标题】:object of nonetype has no len() in a function without len() [closed]nonetype 的对象在没有 len() 的函数中没有 len() [关闭]
【发布时间】: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):

标签: python list nonetype


【解决方案1】:

我只能猜测,因为您没有提供回溯,但看起来您调用 constant_pension 函数的位置可能类似于:

funds = constant_pension(salary_rate, savings, growth, len(retirement))

retirementNone。 (名字可能全都错了,但希望你能明白。)

【讨论】:

    猜你喜欢
    • 2013-03-14
    • 1970-01-01
    • 1970-01-01
    • 2019-10-28
    • 1970-01-01
    • 2021-03-03
    • 2012-08-02
    • 2015-07-30
    • 1970-01-01
    相关资源
    最近更新 更多