I try to make a program that print the factors of a number and print its abundant factors. But when im working with it, i found some problem. I don't know why the output is different if i declare a variable named "sum_abundant_factor" inside the for loop and outside the for loop.
"sum_abundant_factor" is a variable that i use to check whether the factor is abundant or not. (abundant number is a number that is smaller than the sum of its proper divisors).
This is my code and output when i declare "sum_abundant_factor" inside the for loop:
input_number = int(input('Input number : '))
factor = ''
sum_factor = 0
abundant_factor = ''
for i in range(1, input_number+1):
if input_number % i == 0:
sum_abundant_factor = 0
factor += str(i) + ' '
if i < input_number :
sum_factor += i
for j in range(1, i):
if i % j == 0:
sum_abundant_factor += j
if sum_abundant_factor > i:
abundant_factor += str(i) + ' '
print('Factors of {} :'.format(input_number), factor)
print('Abundant Factors :', abundant_factor)
Output :
Input number : 54
Factors of 54 : 1 2 3 6 9 18 27 54
Abundant Factors : 18 54
And this is my code and output when i declare "sum_abundant_factor" before (outside) the for loop:
input_number = int(input('Input number : '))
factor = ''
sum_factor = 0
abundant_factor = ''
sum_abundant_factor = 0
for i in range(1, input_number+1):
if input_number % i == 0:
factor += str(i) + ' '
if i < input_number:
sum_factor += i
for j in range(1, i):
if i % j == 0:
sum_abundant_factor += j
if sum_abundant_factor > i:
abundant_factor += str(i) + ' '
print('Factors of {} :'.format(input_number), factor)
print('Abundant Factors :', abundant_factor)
Output :
Input number : 54
Factors of 54 : 1 2 3 6 9 18 27 54
Abundant Factors : 6 9 18 27 54
I have no idea why the abundant factors output is different when i declaring the variable inside and outside for loop. Can anyone help me explain it to me?
If the sum_abundant_factor = 0
is declared inside the loop, every time it resets the sum_abundant_factor
to 0
when it is the next element of the range
.
Here is an easier example:
a = [1, 2, 3]
for i in a:
b = 0
b += i
print(b)
Every time it is the next element in the a
list, it will reset b
to 0
, so then 0
added to i
would change nothing to the iterator i
, so the above code would output:
1
2
3
Whereas if you define the b
variable outside the loop:
a = [1, 2, 3]
b = 0
for i in a:
b += i
print(b)
It will output:
1
3
6
Because it won't reset, it will keep adding, so 0 + 1
is 1
(the first printed value), and 1 + 2
is 3
(the second printed value) and 3 + 3
is 6
(the third printed value).
If you put the sum_abundunt_factor
inside the for loop, every time when you loop through it, it will be set to 0 if the if input_number % i == 0:
happens, even though you have code such as sum_abundant_factor += j
. It outputs that number, but then resets to 0 the next time you loop through it when the if statement happens. If you leave it outside, the number will only be added every time sum_abundant_factor += j
occurs. The number won't be reset to 0 as it is outside the for loop
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.