gpt4 book ai didi

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

转载 作者:IT老高 更新时间:2023-10-28 20:33:01 28 4
gpt4 key购买 nike

我正在使用 Python 3.2 并在用户输入他们不想继续后尝试退出它,是否有代码会在 while 循环内的 if 语句中退出它?我已经尝试过使用 exit()sys.exit()sys.quit()quit(),然后引发 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

关于python - 如何在if语句中退出python脚本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17179615/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com