X = int(input('Press 1 to run the code and Press 2 to exit: '))
class main:
pass
#here I want the program to quit
if X == 1:
main
else:
quit()发布于 2021-05-27 22:23:43
你不能。代码从上到下运行,python“将”执行类的主体(与函数相反)。因此,如果您退出那里,程序就会终止。
我们不使用类来控制程序的流程。如果你想实现这一点,请改用函数。
另外,如果您在最后一个if之后没有任何代码,那么就没有必要使用quit()。不管怎样,它都会退出。
发布于 2021-05-27 22:25:04
如果你想创建一个运行某些代码的对象,那么为什么不创建一个函数,而不是使用类呢?类通常用于创建多个对象,而不是用来退出程序。
https://stackoverflow.com/questions/67723971
复制相似问题