如果条件不满足,如何退出 python 控制台

How to exit python console if condition does not satisfies

我想输入一个整数作为用户密码,如果密码不匹配,python 控制台应该关闭。

exit() 命令可以提供帮助。参考以下代码:

password = int(input())
your_password = 123
if password!=your_password:   #match the password
    exit()          #exit if password does not match
print("Welcome")

您可以使用 sys 模块中的 exit() 功能。

通常使用非零退出来让服务的调用者知道程序失败退出。

import sys
if notValid(password):
    sys.exit(1)