While 循环内部的 var 如何拦截 Print 函数并在 var 外部进行操作?

我是 Python 新手,但很难理解以下 While 循环代码,因为它的行为非常不同。我知道这段代码有效,但我不知道它是如何工作的。顶级 Python 专家也不知道。


x = 1

while x < 10:

    print x

    x = x + 1 ## How does this VAR manipulate Print as the VAR comes late in the code?

我不知道这是否与控制流或全局变量有关。请帮助我更深入地了解。


SMILET
浏览 146回答 3
3回答

慕侠2389804

x = 1 #Declares the variablewhile x < 10: #checks whether the value of x is less than 10 every time the loop runs&nbsp; &nbsp; print x #prints value of x for the each iterations(1 for first iteration, 2 for second and so on)&nbsp; &nbsp; x = x + 1 #this is the update part让我再告诉你一件事,你这里没有任何全局变量的情况。如果您在理解它是全局声明还是本地声明时遇到困难,我建议您点击此链接
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Python