Emulate a do-while loop in Python?

后端 未结 16 907
伪装坚强ぢ
伪装坚强ぢ 2020-11-22 06:47

I need to emulate a do-while loop in a Python program. Unfortunately, the following straightforward code does not work:

list_of_ints = [ 1, 2, 3 ]
iterator =         


        
16条回答
  •  遇见更好的自我
    2020-11-22 07:28

    If you're in a scenario where you are looping while a resource is unavaliable or something similar that throws an exception, you could use something like

    import time
    
    while True:
        try:
           f = open('some/path', 'r')
        except IOError:
           print('File could not be read. Retrying in 5 seconds')   
           time.sleep(5)
        else:
           break
    

提交回复
热议问题