比方說,我有一個類:從類中停止程序在Python
class A:
def command(self, input):
if command == 'test1':
do something...
if command == 'quit':
#stop the program
和類外我開始while循環:
if __name__ == "__main__"
lst = A()
while True:
command = input("Enter an option:")
lst.command(command)
我被困在點如果命令是「退出」,我需要停止整個程序的運行。我現在面臨的問題是,由於我在課堂外發起了一個while循環,我該如何'假'循環才能停止整個程序?
我正在尋找一個休息方法或可能創建另一個函數來完全退出程序,但不是sys()
或quit()
方法,我猜我不允許這樣做。
['sys.exit()'](https://docs.python.org/3/library/sys.html#sys.exit) – Nayuki