-1

In the following code I keep getting the response that object of NoneType has no len(), but there is no length function anywhere in the code- does anyone know what is wrong?

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
user2906979
  • 307
  • 2
  • 3
  • 11
  • 4
    The problem is probably where the function is called. (And raise exceptions on invalid input!) – Ry- Nov 03 '13 at 16:38
  • I have been calling it using valid input- where could the problem lie in calling the function? – user2906979 Nov 03 '13 at 16:40
  • 3
    Anywhere `len` is used. Can you please show where it’s called and the stack trace for the error? – Ry- Nov 03 '13 at 16:43
  • 2
    If you want help finding the error, show us the traceback. – Ethan Furman Nov 03 '13 at 16:44
  • 1
    The given function works fine. Please provide the traceback given by python when it encounters the error. – shad0w_wa1k3r Nov 03 '13 at 16:46
  • it looks like similar problem, have a look http://stackoverflow.com/questions/11816844/weird-object-of-type-nonetype-has-no-len-error – Gaurav Nov 03 '13 at 16:50
  • Questions concerning problems with code you've written must describe the specific problem — and include valid code to reproduce it — in the question itself. See SSCCE.org for guidance. – Marcin Nov 03 '13 at 16:52
  • 2
    Please, avoid `while` loops when they can be easily replaced by `for` loops. If you want to loop on all numbers between `0`(inclusive) and `years`(exclusive) simply do `for i in range(years):`. – Bakuriu Nov 03 '13 at 16:53
  • why if for better than while? – user2906979 Nov 03 '13 at 17:14
  • [Works okay](http://codepad.org/cyFUqRN7), needs code – Ry- Nov 03 '13 at 17:30

1 Answers1

1

I can only guess since you haven't provided the traceback, but it looks like where you call the constant_pension function is probably something like:

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

and retirement is None. (The names are probably all wrong, but hopefully you get the idea.)

Ethan Furman
  • 63,992
  • 20
  • 159
  • 237