
一种方法是:
sys.exit(0)
您
import sys当然必须。
另一种方法是
break走出无限循环。例如,您可以这样做:
while True: choice = get_input() if choice == "a": # do something elif choice == "q": break
还有另一种方法是将主循环放入函数中并使用
return:
def run(): while True: choice = get_input() if choice == "a": # do something elif choice == "q": returnif __name__ == "__main__": run()
run()使用时需要该函数的唯一原因
return是(与某些其他语言不同)您不能直接
return从Python代码的主要部分(该部分不在函数内部)。
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)