修改循环内的 python 变量
Modify a python variable which is inside a loop
我一直在尝试编写需要修改循环内变量的代码。
a = 1
b = 2
while a<b:
print(b)
现在我想在循环 运行 时更改 b 的值。
我试图找到答案,但无法找到答案。
有没有可能我使用这个初始值为 b 的循环并自动终止这个循环并在变量 b 被修改为其他东西时开始一个新循环?
那怎么可能?
注意:我想从外部修改var b。
进一步说明:
代码的设计方式应该是,当循环已经 运行 时,可以修改用于检查 while 循环条件的变量。
假设用户想将此 while 循环用作函数,他在函数中传递变量以启动循环。现在他想改变在while循环条件中使用的变量值(在上面的例子中是var b)。
def start_loop(a,b):
while a<b:
#more codes will be included here
print('The loop is running infinitely')
#call the function
start_loop(1,2)
Consol: The loop is running infinitely....
循环是 运行 无限,因为循环已经是 运行 我不能要求 while 循环改变 var b 的值。
我正在寻找可以处理这种情况的解决方案。
当循环已经 运行?
时,用户如何从外部修改“var b”
或者如何使用 var b 的修改值调用同一个 start_loop() 函数?这样做应该会导致旧循环的终止和新循环的开始。
希望我能在这里找到答案:)
谢谢
我一直在尝试编写需要修改循环内变量的代码。
a = 1
b = 2
while a<b:
print(b)
现在我想在循环 运行 时更改 b 的值。 我试图找到答案,但无法找到答案。 有没有可能我使用这个初始值为 b 的循环并自动终止这个循环并在变量 b 被修改为其他东西时开始一个新循环? 那怎么可能? 注意:我想从外部修改var b。
进一步说明:
代码的设计方式应该是,当循环已经 运行 时,可以修改用于检查 while 循环条件的变量。 假设用户想将此 while 循环用作函数,他在函数中传递变量以启动循环。现在他想改变在while循环条件中使用的变量值(在上面的例子中是var b)。
def start_loop(a,b):
while a<b:
#more codes will be included here
print('The loop is running infinitely')
#call the function
start_loop(1,2)
Consol: The loop is running infinitely....
循环是 运行 无限,因为循环已经是 运行 我不能要求 while 循环改变 var b 的值。 我正在寻找可以处理这种情况的解决方案。 当循环已经 运行?
时,用户如何从外部修改“var b”或者如何使用 var b 的修改值调用同一个 start_loop() 函数?这样做应该会导致旧循环的终止和新循环的开始。
希望我能在这里找到答案:) 谢谢