2012-03-18 60 views
-2

如何在if中終止程序? 下面是一個例子:如何在Python中退出程序if

#some code 
if a == exit: 
    #here should be the program terminated not quit with program (can be with message) 
else: 
    #the rest of the code 

使用

sys.exit() 

與您正在運行的腳本退出程序。 我需要一個終止腳本的命令。

回答

0

這適用於我。

def main(): 
    print "Hello" 
    testv=5 
    if testv==6: 
     print "!!!" 
     return 0; 
    else: 
     print "World" 
    print "***********" 
    return 0; 

if __name__ == "__main__": 
    main() 

更改testv的值以查看程序是否退出。