如何在if语句中退出python脚本 [英] how to exit a python script in an if statement

查看:100
本文介绍了如何在if语句中退出python脚本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 Python 3.2 并尝试在用户输入他们不想继续后退出它,是否有代码可以在 while 循环内的 if 语句中退出它?我已经尝试过使用 exit()sys.exit()sys.quit()quit(),并引发 SystemExit.

I'm using Python 3.2 and trying to exit it after the user inputs that they don't want to continue, is there code that will exit it in an if statement inside a while loop? I've already tried using exit(), sys.exit(), sys.quit(), quit(), and raise SystemExit.

推荐答案

这对我来说很好:

while True:
   answer = input('Do you want to continue?:')
   if answer.lower().startswith("y"):
      print("ok, carry on then")
   elif answer.lower().startswith("n"):
      print("sayonara, Robocop")
      exit()

在 python 3.2 中使用 input 而不是 raw_input

edit: use input in python 3.2 instead of raw_input

这篇关于如何在if语句中退出python脚本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆