1

I am working on learning recursion and have written this bit of code to return the sum of all numbers from one up to the chosen number. When I run the program, no matter what number I initially enter, the sum it gives me is always "None", and by that I mean, the value of the outputSum variable seems to be "None" in my program. I thought changing the initialized value of SUM_OF_NUMBERS might change this, but it didn't, I still got "None" as the output. Can anyone point to the part in my code that is causing this bug?

def sumOfNumbers(number):

    SUM_OF_NUMBERS = 0

    if number > 0:
        SUM_OF_NUMBERS = SUM_OF_NUMBERS + number
    else:
        return SUM_OF_NUMBERS

    number = number - 1

    sumOfNumbers(number)

def main():

    repeat = 'Y'
    outputSum = 0

    while repeat == 'Y' or repeat == 'y':    
        print("Welcome to the Sum Of Numbers program!")
        number = int(input("\nPlease enter a number to sum up: "))

        outputSum = sumOfNumbers(number)

        print("\nThe sum of all numbers from 1 to " + str(number) + \
              " is " + str(outputSum))

        repeat = input("\nWould you like to sum up another number?" \
                       '\nEnter "Y" for "YES" or "N" for "NO": ')

        if repeat == 'N' or repeat == 'n':
            print("\nThank you for using the program.")
        else:
            print("\nSorry, that was not a valid option.")
            repeat = input('Please enter "Y" for "YES" or "N" for "NO": ')

main()

5 Answers5

2

You were getting None since you were not returning anything when the input is greater than 0.

Much simpler than what you have.

def sumOfNumbers(number):
    if number > 0:
        return number + sumOfNumbers(number-1)
    else:
        return 0
R Sahu
  • 204,454
  • 14
  • 159
  • 270
  • That's a much more elegant solution, thank you. I didn't realize it was skipping over returning anything when the input was greater than zero. –  Jun 05 '14 at 00:49
1
def sumOfNumbers(n):
  if n>0:
    return n+sumOfNumbers(n-1)
  else:
    return 0

i = input("Enter a number")
print "Sum of numbers:",sumOfNumbers(i)
0

Alternately, just pre-test for boundary condition, and handle (tail) recursion,

def sumOfNumbers(number):
    if number <= 0: return 0
    return number+sumOfNumbers(number-1)
ChuckCottrill
  • 4,360
  • 2
  • 24
  • 42
0

Your logic is sound. The problem is that you have reset SUM_OF_NUMBERS to 0 every time. Also, it returns None because you forgot the final return statement. To solve this, you can pass it in as an argument like:

def sumOfNumbers(number, SUM_OF_NUMBERS = 0):

    if number > 0:
        SUM_OF_NUMBERS = SUM_OF_NUMBERS + number
    else:
        return SUM_OF_NUMBERS

    number = number - 1

    return sumOfNumbers(number, SUM_OF_NUMBERS)
Fabricator
  • 12,722
  • 2
  • 27
  • 40
  • 1
    I knew that was a bug and was most likely going to return zero every time when I ran it, but I was more stumped by it returning None so I thought to address that first. Thanks! –  Jun 05 '14 at 00:52
0

There are two small things:

  1. your function returns nothing
  2. you set SUM_OF_NUMBERS = 0 every time you call the function.

Just to simplify here a short code:

def recursive_sum(num):
  if num == 0:
    return 0
  return num + recursive_sum(num-1)
JJR
  • 131
  • 7