Python首先执行底线,然后使用while执行top
我的代码要求我先执行底部的行,然后执行顶部的行。 这是我的密码:Python首先执行底线,然后使用while执行top,python,while-loop,Python,While Loop,我的代码要求我先执行底部的行,然后执行顶部的行。 这是我的密码: test = True while test: if test==False: print("Now executing top lines of the code") break elif test==True: print("Now executing bottom lines of the code") test = False continu
test = True
while test:
if test==False:
print("Now executing top lines of the code")
break
elif test==True:
print("Now executing bottom lines of the code")
test = False
continue
我的预期产出:
Now executing bottom lines of the code
Now executing top lines of the code
不知何故,我的代码不起作用。要么停止打印,要么进入无限循环 我相信您正在寻找这样的产品:
test = True
while True:
if test==False:
print("Now executing top lines of the code")
break
elif test==True:
print("Now executing bottom lines of the code")
test = False
continue
您有这样的代码行为,因为在第一次迭代之后,您将test设置为False(test=False),并且它通过“whiletest”(现在test为False)条件退出循环。因此,您从未进行过循环的第二次迭代您用于条件的
test
变量也是循环的变量,因此一旦它变为False
,循环将退出。。。