如何 运行 仅在 while 循环中执行 1 次操作

How to run a action 1 time ONLY in while loop

我只想在 while 循环中 运行 1 次。

while True:
    print("Hello World")#this word always print
    print("HI")#this word print 1 time only

我不知道你的用例是什么......但只需添加一个“break”来结束循环。

while True:
    print("Hello World")
    print("This words should show 1 time")
    break

根据您的评论进行编辑...

ran = False
while True:
    print("Hello World")
    If not ran
        print("This words should show 1 time")
        ran = True

只需添加验证逻辑即可

x = False
while True:
    print("Hello World")
    if x == False:
        print("HI")
        x = True