替换第二个for循环后,为什么会改变这个for循环中的输出变量?

huangapple go评论71阅读模式
英文:

Why does replacing an output variable after second for loop change the output in this for-loop?

问题

我是新手程序员,学习Python作为我的第一门语言。我尝试调试这段代码以理解它的工作原理。但是我只能理解代码的50%。我有疑问,为什么在第二个for循环(在范围循环内)之后我不能替换(输出空变量)以获得相同的输出。但如果我这样做,输出就会变得不同。请帮助我理解这段代码。

numbers = [5,2,5,2,2]
for x_count in numbers:
    output = ''
    for count in range(x_count):
        #output = ''
        output = output + 'x'
    print(output)

我需要理解这段代码。

英文:

I am new to programming and learn Python as my first language. I try to debug this code to understand how it works. But I can only understand the code 50%. I have doubt here why I can not replace (output empty variable) after second for loop (in range loop) to get same output. But If I do that, It becomes different output. Please help me to understand this code.

numbers = [5,2,5,2,2]
for x_count in numbers:
    output = ''
    for count in range(x_count):
        #output = ''
        output = output + 'x'
    print(output)

xxxxx
xx
xxxxx
xx
xx

I need to understand this code

答案1

得分: 1

numbers = [5,2,5,2,2]
for x_count in numbers:          
    output = ''
    for count in range(x_count): 
        output = output + 'x'    
    print(output)
英文:
numbers = [5,2,5,2,2]
for x_count in numbers:          #create a loop for each value in 'numbers'
    output = ''                  #declare variable
    for count in range(x_count): #next loop, where 'range' make 'x_count' cycles 
        #output = ''             #do nothing, but if it works. then on each cycle it will re-declare a variable, which means deleting its previous value 
        output = output + 'x'    #add 'x' on each cycle 'next loop'
    print(output)

答案2

得分: 0

numbers = [5,2,5,2,2]
for x_count in numbers:
    output = ''
    for count in range(x_count):
        output = output + 'x'
    print(output)

我找到了我的问题的答案这是答案当我在第二个内部循环后提及输出空字符串变量时内部循环会重复地将输出制作成只有一个'x'这意味着当内部for循环运行在数字列表中的第一个数字的范围上时例如(0,1,2,3,4)输出变量只变成一个'x'因为输出的空变量将数字的范围变成了只有一个'x'这就是为什么它在列中只显示一个'x'五次请查看下面的输出

输出

    x
    x
    x
    x
    x
英文:

numbers = [5,2,5,2,2]
for x_count in numbers:
output = ''
for count in range(x_count):
#output = ''
output = output + 'x'
print(output)

I found the answer for my question. Here is the answer. When I mention output empty string variable after second inner loop, the inner loop repeatedly make output with 'x to the only one 'x. That means, when inner for loops runs over the range of first number from numbers list, for example :(0,1,2,3,4) the the output variable with 'x' only becomes one 'x'. Because the output empty variable change the range of number into only one 'x. That's why It shows only one 'x' 5 times in column. check the output below.

output :

x
x
x
x
x

huangapple
  • 本文由 发表于 2023年5月29日 02:02:35
  • 转载请务必保留本文链接:https://go.coder-hub.com/76352902.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定