[Tutor] Help

Cameron Simpson cs at cskk.id.au
Wed Oct 7 17:29:23 EDT 2020


On 07Oct2020 22:16, Gilbert Makgopa <gmakgopa at gmail.com> wrote:
>Kindly assist with the function below.
>The task is to write a function so that it returns the sum of all the
>divisors of a number, without including it. A divisor is a number that
>divides into another without a remainder.
>
>def sum_divisors(n):
>  sum = 0
>  div =0
>  # Return the sum of all divisors of n, not including n
>  while (div > 0 and div!=n):
>    sum = sum + div
>    return sum

Look at the indentation; Python relies on indentation to know what
control structures enclose a statement. Are all the statements in the
right place? Consider each individually, carefully.

Cheers,
Cameron Simpson <cs at cskk.id.au>

>print(sum_divisors(0))
># 0
>print(sum_divisors(3)) # Should sum of 1
># 1
>print(sum_divisors(36)) # Should sum of 1+2+3+4+6+9+12+18
># 55
>print(sum_divisors(102)) # Should be sum of 2+3+6+17+34+51
># 114


More information about the Tutor mailing list