Emulate a do-while loop in Python?

后端 未结 16 1064
伪装坚强ぢ
伪装坚强ぢ 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:19

    My code below might be a useful implementation, highlighting the main difference between do-while vs while as I understand it.

    So in this one case, you always go through the loop at least once.

    first_pass = True
    while first_pass or condition:
        first_pass = False
        do_stuff()
    

提交回复
热议问题