1 votes

Finding multiples of a number and adding them in Python

I am starting on the python topic and I have an exercise that already broke my head. Sure it's simple but I can't find the error. I have to find the numbers (with a function using the while loop) in which a number (n) is divisible and add them without counting "n". that is, if "n = 8" it should give 7 (1 + 2 + 4), but it gives me 1. :( If someone gives me a hand I would appreciate it.

 def sum_divisors(n):
  sum = 0
  div = 1
  if n==0:
   return 0
  elif n>0:
       while div<n:
          if n%div==0:
           sum=sum+div
           div+=1
           return sum
          else:
            div+=1
            return div
      
 
print(sum_divisors(8))

HolaDevs.com

HolaDevs is an online community of programmers and software lovers.
You can check other people responses or create a new question if you don't find a solution

Powered by:

X