Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/330.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 为循环内部和外部声明变量的区别_Python_For Loop - Fatal编程技术网

Python 为循环内部和外部声明变量的区别

Python 为循环内部和外部声明变量的区别,python,for-loop,Python,For Loop,我试图制作一个程序,打印一个数字的因子,并打印其丰富的因子。但是当我使用它时,我发现了一些问题。如果我在for循环内部和for循环外部声明一个名为“sum\u\u factor”的变量,我不知道为什么输出会不同 “总和富足因子”是我用来检查因子是否富足的变量。(富足数是一个小于其适当除数之和的数) 这是我在for循环中声明“sum\u factor”时的代码和输出: input_number = int(input('Input number : ')) factor = '' sum_fact

我试图制作一个程序,打印一个数字的因子,并打印其丰富的因子。但是当我使用它时,我发现了一些问题。如果我在for循环内部和for循环外部声明一个名为“sum\u\u factor”的变量,我不知道为什么输出会不同

“总和富足因子”是我用来检查因子是否富足的变量。(富足数是一个小于其适当除数之和的数)

这是我在for循环中声明“sum\u 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
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
input\u number=int(输入('input number:'))
因子=“”
总和系数=0
富余系数=“”
对于范围内的i(1,输入_编号+1):
如果输入\u编号%i==0:
总和系数=0
因子+=str(i)+”
如果ii:
丰富的_因子+=str(i)+”
打印({}:'的系数。格式(输入\编号),系数)
打印('丰富因子:',丰富因子)
输出:
输入号码:54
因素54:12369182754
丰富因素:18 54
这是我在for循环之前(外部)声明“sum\u-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
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
input\u number=int(输入('input number:'))
因子=“”
总和系数=0
富余系数=“”
总和系数=0
对于范围内的i(1,输入_编号+1):
如果输入\u编号%i==0:
因子+=str(i)+”
如果ii:
丰富的_因子+=str(i)+”
打印({}:'的系数。格式(输入\编号),系数)
打印('丰富因子:',丰富因子)
输出:
输入号码:54
因素54:12369182754
丰富因素:69182754

我不知道为什么在为循环声明内部和外部变量时,输出的丰富因子是不同的。有人能帮我解释一下吗?

如果在循环中声明了
sum\u-abundant\u factor=0
,每次当它是
范围的下一个元素时,它都会将
sum\u-abundant\u factor
重置为
0

下面是一个更简单的例子:

a = [1, 2, 3]
for i in a:
    b = 0
    b += i
    print(b)
每次它是
a
列表中的下一个元素时,它都会将
b
重置为
0
,因此添加到
i
0
不会对迭代器
i
进行任何更改,因此上述代码将输出:

1
2
3
而如果在循环外定义
b
变量:

a = [1, 2, 3]
b = 0
for i in a:
    b += i
    print(b)
它将输出:

1
3
6

因为它不会重置,所以它将继续添加,因此
0+1
1
(第一个打印值),
1+2
3
(第二个打印值),
3+3
6
(第三个打印值)。

如果将
求和因子
放在for循环内,每次循环时,如果发生
if input\u number%i==0:
,即使您有
sum\u factor+=j这样的代码,也会将其设置为0。它输出该数字,但当if语句发生时,下一次循环该数字时,它将重置为0。如果将其放在外部,则仅在每次出现
sum\u factor+=j
时才会添加该数字。数字不会重置为0,因为它在for循环之外

对于每个循环,
sum\u factor
最初将设置为0。在循环外部声明时,它将保持其值,并且最初不会为0。你一直在增加它的值,但每次运行前都不会重置。哦,我明白了,谢谢你的解释。np!希望你学到一些东西!