Python while set变量做什么?
假设我有一个集合,我在集合时做:像下面这样,循环什么时候停止Python while set变量做什么?,python,Python,假设我有一个集合,我在集合时做:像下面这样,循环什么时候停止 x={'3', '4', '2'} while x: print(1) while如果您从未在while主体中修改任何非空变量,则该变量将永远不会停止。永远不会 因为while计算x的真实性,这是真的-因此有一个无限循环 您可以自己尝试或查看文档:要了解代码的作用,您需要了解while语句的工作原理 while语句是一个布尔语句 这意味着(真、假) 例如: x = True while x: # without saying
x={'3', '4', '2'}
while x:
print(1)
while
如果您从未在while主体中修改任何非空变量,则该变量将永远不会停止。永远不会
因为while
计算x的真实性,这是真的-因此有一个无限循环
您可以自己尝试或查看文档:要了解代码的作用,您需要了解while语句的工作原理
while
语句是一个布尔语句
这意味着(真、假)
例如:
x = True
while x: # without saying true or false here "while" assumes you are checking for true.
print ("X is True")
x = False
x={'3', '4', '2'}
while x:
print(1)
x = True
while x:
print (1)
x = 0
上述代码将在其中运行命令,直到x==False
。
因此,代码将运行一次,因为我在语句末尾说了x=False
以上代码与所说的相同
x = True
while x is True:
print ("X is True")
x = False
在您的示例中:
x = True
while x: # without saying true or false here "while" assumes you are checking for true.
print ("X is True")
x = False
x={'3', '4', '2'}
while x:
print(1)
x = True
while x:
print (1)
x = 0
x
将始终为True
,如果不将其更改为False或等于False的值。
因此你的循环永远不会结束“muahahahahaha”
例如:
x = True
while x: # without saying true or false here "while" assumes you are checking for true.
print ("X is True")
x = False
x={'3', '4', '2'}
while x:
print(1)
x = True
while x:
print (1)
x = 0
或:
这将只打印出一次数字
1
,因为x已被分配0
,对于while语句,它相当于False。您尝试过吗?whilex
的计算结果为True
(即x
不是一个空集{}
)打印(1)
。如果x
从未更改,则这是一个无限循环。