[Tutor] Help

yasir arfat arfatyasir354 at gmail.com
Wed Oct 7 17:38:37 EDT 2020


#function will return sum of divisors of
 #    n(excluding n)
def sum_div(n):
      sum=0
# loop will iterate from 1 to less than n
      for i in range (1,n):
#checking whether n is dividable by i that
# that means n%i will return 0 that is true for
# i ( divisor) of n
         if n%i==0:
# adding value to sum variable
            sum+=i
# finally return
      return sum

On Thu, Oct 8, 2020, 02:29 Cameron Simpson <cs at cskk.id.au> wrote:

> 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
> _______________________________________________
> Tutor maillist  -  Tutor at python.org
> To unsubscribe or change subscription options:
> https://mail.python.org/mailman/listinfo/tutor
>


More information about the Tutor mailing list