[英]Difference of declaring variable inside for loop and outside for loop
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.
如果我在 for 循环内部和 for 循环外部声明一个名为“sum_abundant_factor”的变量,我不知道为什么 output 会有所不同。
"sum_abundant_factor" is a variable that i use to check whether the factor is abundant or not. “sum_abundant_factor”是我用来检查因子是否丰富的变量。 (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:这是我的代码和 output 当我在 for 循环中声明“sum_abundant_factor”时:
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:这是我的代码和 output 当我在 for 循环之前(外部)声明“sum_abundant_factor”时:
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.我不知道为什么当我在循环内部和外部声明变量时,丰富的因素 output 是不同的。 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
.如果在循环内声明了
sum_abundant_factor = 0
,则每次当 sum_abundant_factor 是range
的下一个元素时,它都会将sum_abundant_factor
重置为0
。
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:每次它是
a
列表中的下一个元素时,它会将b
重置为0
,因此添加到i
的0
不会改变迭代器i
,因此上面的代码将为 output:
1
2
3
Whereas if you define the b
variable outside the loop:而如果您在循环外定义
b
变量:
a = [1, 2, 3]
b = 0
for i in a:
b += i
print(b)
It will output:它将 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).因为不会重置,所以会一直加,所以
0 + 1
就是1
(第一个打印值), 1 + 2
就是3
(第二个打印值), 3 + 3
就是6
(第三个打印值)。
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
.如果将
sum_abundunt_factor
放在 for 循环中,则每次循环时,如果if input_number % i == 0:
发生,它将设置为 0,即使您有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 语句发生时循环遍历它时会重置为 0。 If you leave it outside, the number will only be added every time
sum_abundant_factor += j
occurs.如果你把它放在外面,这个数字只会在每次
sum_abundant_factor += j
出现时添加。 The number won't be reset to 0 as it is outside the for loop该数字不会重置为 0,因为它在 for 循环之外
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.