How to break a Python while loop from a function within the loop

后端 未结 5 1573
时光说笑
时光说笑 2021-01-15 03:05
while True:
    input = raw_input(\"enter input: \")
    result = useInput(input)

def useInput(input):
    if input == \"exit\":
        break   #return 0 / quit /          


        
5条回答
  •  死守一世寂寞
    2021-01-15 03:43

    1. Raise an exception, that you can handle outside the While loop
    2. Return a flag to be captured by the caller and handle accordingly. Note, "if logic" directly in the while loop,, would be the most preferred way.

提交回复
热议问题