others - python 3.2如何在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")



时间:

使用sys.exit()raise SystemExit


import sys


for trial in range(3):


 if input('Password:') == 'password':


 break


else:


 sys.exit()


print ("Welcome in"))



编辑:要结束它,在try-except块中自动换行:


try:


 import sys


 for trial in range(1):


 if raw_input('Password:') == 'password':


 break


 else:


 raise SystemExit #or just sys.exit()


 print ("Welcome in")


except SystemExit:


 pass #when the program throws SysExit do nothing here,i.e end silently



然后调用该函数运行你的程序。


def main():


 for trial in range(3):


 if input('Password:') == 'password':


 break


 else:


 return


 print ("Welcome in")



main()



sys.exit可以随时退出程序。

我尝试了sys.exit(),它把'sys'作为INVALID SYNTAX

...