Bub*_*ter 1 python if-statement
下面我的代码很有用,但是,如果我选择'1',代码将运行if语句并打印"success",但它也将运行else语句并退出.如果用户选择1,2或3,如何停止运行else语句?
谢谢!
print "\nWelcome to the tool suite.\nWhich tool would you like to use?\n"
print "SPIES - press 1"
print "SPADE - press 2"
print "SPKF - press 3"
print "For information on tools - press i\n"
choice = raw_input("")
if choice == "1":
execfile("lot.py")
print ("success")
if choice == "2":
#execfile("spade.py")
print ("success")
if choice == "3":
#execfile("spkf.py")
print ("success")
if choice == "i":
print "Google Maps is awesome.\n"
print "SPADE\n"
print "SPKF\n\n"
else:
print "Error, exiting to terminal"
exit(1)
Run Code Online (Sandbox Code Playgroud)
你想要这个elif结构.
if choice == "1":
...
elif choice == "2":
...
else: # choice != "1" and choice != "2"
...
Run Code Online (Sandbox Code Playgroud)
否则,不同的if语句彼此断开.我添加了一个空白行强调:
if choice == "1":
...
if choice == "2":
...
else: # choice != 2
...
Run Code Online (Sandbox Code Playgroud)