如何在if语句中退出python脚本

2024-04-26 00:02:28 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在使用Python3.2,并试图在用户输入他们不想继续的内容后退出它,是否有代码会在while循环中的if语句中退出它?我已经试过使用exit()sys.exit()sys.quit()quit(),并提升SystemExit


Tags: 代码用户内容ifsysexit语句quit
1条回答
网友
1楼 · 发布于 2024-04-26 00:02:28

这对我很有用:

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

相关问题 更多 >