[Tutor] Question Regarding Quiz Material
Marc Tompkins
marc.tompkins at gmail.com
Tue Jun 1 10:46:41 EDT 2021
On Tue, Jun 1, 2021 at 7:21 AM Nick Yim <theoldny at gmail.com> wrote:
> I am wondering why the numbers - n = 3, n = 36, n 102 - are not included in
> the list of divisors?
>
> This line:
while n!=0 and x<n:
specifies that x (the divisor you're checking) MUST be smaller than your
original number. If you want to include the original number as its own
divisor, change it to
while n!=0 and x<=n:
> As well, why is the number 1 included as a divisor of 3 and 36, but not as
> a divisor of 102?
>
It _is_ included as a divisor of 102; I think your addition is off.
A small modification to your script will make things a bit clearer:
def sum_divisors(n):
x = 1
#trying all the numbers from 1, because you cannot divide by 0
sum = 0
divisors = []
#adding to 0
while n!=0 and x<n:
#conditions for n to have divisors (not be 0) and for x to be a
potential divisor
#error because the answer does not include the number itself
if n%x == 0:
sum += x
divvs.append(x)
x +=1
return (n, divisors, sum)
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