-2

I'm learning python 3. I expect this program to sum together a decreasing sequence of integers. Instead, I get NONE.

If this was throwing an error I might be able to figure out what I'm doing wrong, but in this case I am stumped.

def add_many_things(init_value):
    accumulator = 0
    while init_value > 0 :
        accumulator = accumulator + init_value
        init_value = init_value - 1

result = add_many_things(37)
print(f"{result}")
Calydon
  • 251
  • 1
  • 9
  • 1
    the function is missing ``return statement``, the default return value is ``None`` – sushanth Jun 01 '20 at 04:38
  • 1
    Where were you expecting any result to come from? The function contains no `return` statement, Python is hardly going to try to guess which variable holds the value of interest. – jasonharper Jun 01 '20 at 04:38

2 Answers2

1

Try this:

def add_many_things(init_value):
    accumulator = 0
    while init_value > 0 :
        accumulator = accumulator + init_value
        init_value = init_value - 1
    return accumulator

result = add_many_things(37)
print(f"{result}")

You miss to add the return statement to the add_many_things function.
Python allows this case and evaluate the return value to None.

Yusef Maali
  • 2,201
  • 2
  • 23
  • 29
0

Add return accumulator to the function. You're trying to assign the result of the function:

result = add_many_things(37)

but your add_many_things() function doesn't return a value. Adding the return statement at the end of the function will pass the value of accumulator back to the caller.

Caleb
  • 124,013
  • 19
  • 183
  • 272
arnab das
  • 135
  • 7
  • This **does** answer the question, but it was automatically flagged as low quality because of the short length. To avoid having that happen in the future, try to provide more complete answers with explanations. – Caleb Jun 01 '20 at 14:25