# 如何获得一个数的所有除数之和，而不包括它How to get the sum of all the divisors of a number, without including it

``````print(func(6)) # Should be 1+2+3=6
print(func(12)) # Should be 1+2+3+4+6=16
``````

``````def func(num):
for i in range(1,num-1):
if num%i==0:
``````

## 4 个回复4

### #1楼 票数：2

``````def func(num):
add = [i for i in range(1, num-1) if num % i == 0]
``````

``````print(func(6))  # Should be 1+2+3=6
print(func(12)) # Should be 1+2+3+4+6=16
``````

### #2楼 票数：2

``````def sum_divisors(n):
sum = 0
x = 1
while n != 0 and x < n :

if n % x == 0  :
sum += x
else:
sum += 0
x += 1

return sum
``````

### #3楼 票数：1 已采纳

``````def func(num):
total_sum = 0
for i in range(1, num - 1):
if num % i == 0:
total_sum += i

``````

### #4楼 票数：0

``````def func(num):
for i in range(1,num-1):
if num%i==0:
``````

``````print(func(6)) # Should be 1+2+3=6
print(func(12)) # Should be 1+2+3+4+6=16
``````

``````def list_func(num):
return sum([i if num%i==0 else 0 for i in range(1, num-1)])
``````

3回复

3回复

4回复

1回复

1回复

5回复

17回复

2回复