這裏是新手蟒蛇。如果用戶選擇「Q」作爲「退出」,我怎樣才能擺脫第二個while循環? 如果我點擊「m」,它會進入主菜單,在那裏我可以退出點擊「Q」鍵。如何擺脫python中的double while循環?
while loop == 1:
choice = main_menu()
if choice == "1":
os.system("clear")
while loop == 1:
choice = app_menu()
if choice == "1":
source = '%s/%s/external' % (app_help_path,app_version_10)
target = '%s/%s' % (target_app_help_path,app_version_10)
elif choice == "2":
source = '%s/%s/external' % (app_help_path,app_version_8)
target = '%s/%s' % (target_app_help_path,app_version_8)
elif choice.lower() == "m":
break
loop = 0
elif choice.lower() == "q":
break
loop = 0
sendfiles(source, target)
# Internal files
elif choice == "q":
loop = 0
應用菜單的方法:
def app_menu()
print "Select APP version"
print "-------------------"
print "1) 11"
print "2) 10"
print "3) 8"
print "m) Main Menu"
print "q) Quit"
print
return raw_input("Select an option: ")
一個小提示:Python有一個布爾類型(true/false),使得代碼比使用1/0作爲布爾值更簡潔。 :) – 2010-10-20 14:33:24
我得看看它。你能告訴我一個例子嗎? – luckytaxi 2010-10-20 14:37:50
他意味着使用'loop = False'而不是'loop = 0',並且'loop = True'而不是'loop = 1'。使它更具可讀性...... 編輯:作爲一個額外的好處,你可以使用'while循環:'而不是'while循環== 1',給你一個更強大的可讀性長劍+2。 – Aphex 2010-10-20 14:39:44