Emulate a do-while loop in Python?

后端 未结 16 877
伪装坚强ぢ
伪装坚强ぢ 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条回答
  •  慢半拍i
    慢半拍i (楼主)
    2020-11-22 07:41

    for a do - while loop containing try statements

    loop = True
    while loop:
        generic_stuff()
        try:
            questionable_stuff()
    #       to break from successful completion
    #       loop = False  
        except:
            optional_stuff()
    #       to break from unsuccessful completion - 
    #       the case referenced in the OP's question
            loop = False
       finally:
            more_generic_stuff()
    

    alternatively, when there's no need for the 'finally' clause

    while True:
        generic_stuff()
        try:
            questionable_stuff()
    #       to break from successful completion
    #       break  
        except:
            optional_stuff()
    #       to break from unsuccessful completion - 
    #       the case referenced in the OP's question
            break
    

提交回复
热议问题