简体   繁体   English

Python代码逐步了解代码

[英]Python Code Understanding the code step by step

Hi everyone I am currently practicing interpreting code and writing down the process of it every step of the way! 大家好,我目前正在练习解释代码并写下代码的过程! This is what I have currently come up with. 这是我目前想出的。

x = 4
y = 19
finished = False
while x <= y and not finished:
    subtotal = 0
    for z in range(0, x, 4):
        print(x)
        subtotal += x
        print("This is subtotal", subtotal)
        if y // x <= 1:
            finished = True
        else:
            x += x
            print("New x value:", x)
  1. x = 4, y = 19, finished = false, subtotal =4, z = 0 x = 4,y = 19,完成=假,小计= 4,z = 0
  2. x = 8, y = 19, finished = false, subtotal =8, z = 0 x = 8,y = 19,完成= false,小计= 8,z = 0
  3. x = 16, y = 19, finished = True, subtotal =24, z = 0 x = 16,y = 19,完成= True,小计= 24,z = 0

I believe what I did here is correct but I am not sure how the subtotal are going to 4 to 8 to 24? 我相信我在这里所做的是正确的,但是我不确定小计将如何从4变为8到24? If someone could explain this to me that would be great. 如果有人可以向我解释这一点,那就太好了。

I understand that range is exclusive so when the x value is 4 it only goes through the for loop once hence why the subtotal is = 4. However when the x value is 8 it goes through the for loop to my under standing 2 times so this is the part where I get lost. 我知道范围是互斥的,因此当x值为4时,它仅通过for循环一次,因此为什么小计=4。但是,当x值为8时,它通过for循环到达我的站立次数2次,因此是我迷路的部分。

My last question is each time it goes through this loop does the subtotal get reset everytime the x value is changed? 我的最后一个问题是,每次经过此循环时,每次x值更改时,小计都会重置吗? Would this be the reason I can not get the correct subtotals? 这是我无法获得正确小计的原因吗?

If someone could visually show this to me or explain it that would be awesome thank you very much! 如果有人可以直观地向我展示或解释这一点,那就太好了,谢谢!

That is because in the first loop subtotal is 0. The for loop iterates just once because it then looks like this for z in range(0, 4, 4) . 这是因为在第一回路小计为0。 for循环迭代仅仅一次,因为它然后看起来像这样for z in range(0, 4, 4) Then x and subtotal become 4. NOW subtotal is brought back to 0 and the for loop becomes for z in range(0, 8, 4) so this time the for loop will iterate twice as there are two possible numbers in that range (which are 0 and 4), subtotal gets added to x which is 8 and x becomes 16, the for loop iterates ( NOTE the subtotal doesn't get brought back to 0 as the subtotal = 0 statement isn't inside the for loop) again making subtotal now 8 + 16. Which is 24. 然后x和小计变成4。现在小计回到0,for循环成为for z in range(0, 8, 4)因此for循环将迭代两次,因为该范围内有两个可能的数字(分别是0和4),小计被添加到x的8和x变成16的for循环中(请注意小计不会被带回到0,因为subtotal = 0语句不在for循环内)现在小计8 + 16,即24。

Just examining the changes to the variables: 只需检查变量的更改:

Start: x = 4, y = 19, finished = False
1.      subtotal = 0
2.       z = 0
3.       subtotal += x (0+4) = 4
4.       x += x (4+4) = 8
5.      subtotal = 0
6.       z = 0
7.       subtotal += x (0+8) = 8
8.       x += x (8+8) = 16
9.       z = 4
10.      subtotal += x (8+16) = 24
11.      finished = True
End:   x = 16, y = 19, finished = True, z = 4, subtotal = 24

subtotal only gets reset to 0 when the inner loop exits, as x gets large the inner loop repeats more times, 1 the first time, 2 the second. subtotal仅在内部循环退出时重置为0 ,因为x变大,内部循环重复多次,第一次重复1次,第二次重复2次。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM