Skip to main content
 首页 » 编程设计

python之如何在空闲状态下停止程序,python 3.2

2025年05月04日12thcjp

我在 Idle 中编写了一个程序,上面写着:

for trial in range(3): 
if input('Password:') == 'password': 
    break 
else: 
    # didn't find password after 3 attempts 
    **I need a stop program here** 
print ("Welcome in") 

记住,这是在 Idle 模式下,所以我需要 Idle 程序,而不是 CMD。如果有帮助,我也在使用 Python 3.2。

请您参考如下方法:

恕我直言,一个更好的方法是将您的程序放入一个函数中,并在您希望它停止时return。然后只需调用该函数即可运行您的程序。

def main(): 
    for trial in range(3): 
        if input('Password:') == 'password': 
            break 
    else: 
        return 
    print ("Welcome in") 
 
main()