One way is to do:
sys.exit(0)
You will have to import sys
of course.
Another way is to break
out of your infinite loop. For example, you could do this:
while True:
choice = get_input()
if choice == "a":
# do something
elif choice == "q":
break
Yet another way is to put your main loop in a function, and use return
:
def run():
while True:
choice = get_input()
if choice == "a":
# do something
elif choice == "q":
return
if __name__ == "__main__":
run()
The only reason you need the run()
function when using return
is that (unlike some other languages) you can't directly return
from the main part of your Python code (the part that's not inside a function).
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…