Python exit()命令触发无限循环(已解决)
当input option=3时,我在运行下面的代码时遇到了无限循环Python exit()命令触发无限循环(已解决),python,function,loops,Python,Function,Loops,当input option=3时,我在运行下面的代码时遇到了无限循环 def enter_number_only(): print() print("======================") print("Enter Number 1-3 only!") print("======================") 但是,如果我替换了: (初始代码) 到 (编辑代码) 它将顺利退出循环 def
def enter_number_only():
print()
print("======================")
print("Enter Number 1-3 only!")
print("======================")
但是,如果我替换了:
(初始代码)
到
(编辑代码)
它将顺利退出循环
def thank_you_goodbye():
print()
print("======================")
print("Thank You & GoodBye!")
print("======================")
exit()
需要关于如何保持初始代码但能够退出循环的建议吗?寻找问题的根源
非常感谢
更新:由于except
条款,该问题通过@Daniel Corin共享解决。代码通过下面的编辑得到修复
except TypeError:
enter_number_only()
只需删除
thanky\u bye
函数最后一行的exit
,它就会正常工作
def thank_you_goodbye():
print()
print("======================")
print("Thank You & GoodBye!")
print("======================")
exit()
许多人都提到了通过删除
exit()
来解决问题的方法,但让我们来深入了解一下为什么您会看到一个更简单的示例:
在下面的代码中,我们首先请求用户输入,然后调用exit()
,然后由bareexcept
子句处理
旁注:不建议使用除条款之外的裸。更多关于这个
为True时:
尝试:
i=输入(“输入”)
退出()
除:
打印(‘嘿’)
我们可能期望在输入第一个用户输入后,程序将在处理except
子句中的SystemExit
后再次循环请求用户输入。然而,实际情况是:
❯ python3 test.py
input? 1
hey
input? hey
input? hey
input? hey
input? hey
...infinitely
但是,如果显式地处理SystemExit
,程序也会执行我们可能不期望的操作——它实际上会退出,引发不同的异常:
为True时:
尝试:
i=输入(“输入”)
退出()
除系统退出外:
打印(‘嘿’)
❯ python3测试2.py
真的
回溯(最近一次呼叫最后一次):
文件“/Users/danielcorin/Desktop/test2.py”,第5行,在
退出()
文件“/usr/local/cillar”/python@3.9/3.9.1_1/Frameworks/Python.framework/Versions/3.9/lib/python3.9/_sitebuiltins.py”,调用中第26行__
升起系统出口(代码)
系统退出:无
在处理上述异常期间,发生了另一个异常:
回溯(最近一次呼叫最后一次):
文件“/Users/danielcorin/Desktop/test2.py”,第7行,在
打印(sys.stdin.isatty())
ValueError:对关闭的文件执行I/O操作
我们仍然处于while循环中,因此所有异常(首先是SystemExit
,然后是ValueError
重复)都由裸露的except
子句处理,因此结果是程序永远打印,直到我们从外部发出终止程序的信号。当在thank\u you\u bye
函数中调用exit()
时,此行为与程序所显示的行为相同
简而言之,exit()
调用中断了input
函数,该函数由于裸露的except
子句而导致无限循环。是否需要exit()
函数?(在Thank you Byed函数中)不需要exit()函数,因为'break'退出while循环,应用程序结束。如果坚持使用exit\,请使用sys.exit()。为什么在退出后需要break
@jeffthechicken exit()函数,以确保在用户输入选项3后退出程序。当输入选项=3时,运行下面的代码时遇到无限循环。什么是无限循环?当我输入3时,它退出
s,因此没有无限循环。这解决了我的问题!根本原因是裸露的除外条款。该问题在添加了的子句后得到解决,除了TypeError:
谢谢,伙计!
except TypeError:
enter_number_only()
def thank_you_goodbye():
print()
print("======================")
print("Thank You & GoodBye!")
print("======================")
exit()
❯ python3 test.py
input? 1
hey
input? hey
input? hey
input? hey
input? hey
...infinitely
❯ python3 test.py
input? 1
hey
input? Traceback (most recent call last):
File "/Users/danielcorin/Desktop/test.py", line 3, in <module>
i = input('input? ')
ValueError: I/O operation on closed file.
❯ python3 test2.py
True
Traceback (most recent call last):
File "/Users/danielcorin/Desktop/test2.py", line 5, in <module>
exit()
File "/usr/local/Cellar/python@3.9/3.9.1_1/Frameworks/Python.framework/Versions/3.9/lib/python3.9/_sitebuiltins.py", line 26, in __call__
raise SystemExit(code)
SystemExit: None
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "/Users/danielcorin/Desktop/test2.py", line 7, in <module>
print(sys.stdin.isatty())
ValueError: I/O operation on closed file