如何跳出循环然后重新开始

0 投票
1 回答
3258 浏览
提问于 2025-04-18 08:54

我想问一下,怎么在某个时刻中断Python的无限循环(while True),然后再从头开始?

while True:
     if a==0:
         print "ok"
     elif a==1:
         break
         #want to start over again
     command = sock.recv(1024)
          if blah blah ==blah:
               .........

1 个回答

3

你可能需要的是 continue 这个关键字。

while True:
     if a==0:
         print "ok"
     elif a==1:
         continue
         # goes back to top of loop
     else:
          print "ok"

撰写回答