[Tutor] Help

Alan Gauld alan.gauld at yahoo.co.uk
Wed Oct 7 20:12:58 EDT 2020


On 07/10/2020 22:25, Alan Gauld via Tutor wrote:

>> 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

> Things wrong:
> 1) You start with div=0 then immediately check if div > 0 which
> it isn't.
> 2) You never modify div or n so the while loop always gives
> the same result.
> 3) You never check if div is actually a diviror of n before
> adding it to sum.

I missed one:

4) You unconditionally return inside the while loop so
it could never run more than once.


-- 
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/
http://www.amazon.com/author/alan_gauld
Follow my photo-blog on Flickr at:
http://www.flickr.com/photos/alangauldphotos




More information about the Tutor mailing list