2010-10-20 82 views
3

這裏是新手蟒蛇。如果用戶選擇「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: ") 
+2

一個小提示:Python有一個布爾類型(true/false),使得代碼比使用1/0作爲布爾值更簡潔。 :) – 2010-10-20 14:33:24

+0

我得看看它。你能告訴我一個例子嗎? – luckytaxi 2010-10-20 14:37:50

+2

他意味着使用'loop = False'而不是'loop = 0',並且'loop = True'而不是'loop = 1'。使它更具可讀性...... 編輯:作爲一個額外的好處,你可以使用'while循環:'而不是'while循環== 1',給你一個更強大的可讀性長劍+2。 – Aphex 2010-10-20 14:39:44

回答

5

你幾乎擁有它;你只需要交換這兩條線。

elif choice.lower() == "m": 
    break 
    loop = 0 

elif choice.lower() == "m": 
    loop = 0 
    break 

在設置loop之前,您跳出了嵌套循環。 :)

+0

確定它看起來像它的工作,但我修改了「q」elif。 – luckytaxi 2010-10-20 15:05:41

1

使用兩個用於兩個迴路不同的變量,如loop1loop2

當你第一次在內循環中按m時,你只是在外面休息,然後你可以單獨處理q。順便說一句,你不應該需要內部變量來保持循環,只需要一個無限循環直到鍵'm'被按下。然後,你從內循環中突破,同時保持第一個。

2

變化

break 
loop = 0 

loop = 0 
break 

在你的elif塊。

1

將您的頂級循環重命名爲mainloop,並在接收到q時設置mainloop = 0。

while mainloop == 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": 
       loop = 0 
       break 
      elif choice.lower() == "q": 
       mainloop = 0break 
       break 
      sendfiles(source, target) 

    # Internal files 

    elif choice == "q": 
     mainloop = 0 
+0

'mainloop = 0'和'loop = 0'永遠不會在你的'elif'塊中被擊中,所以這不會起作用 – 2010-10-20 14:35:03

+0

你忽略了與luckytaxi一樣的小錯誤;看到我的答案。 :) – Aphex 2010-10-20 14:36:16

+0

@Aphex:我不會說*小* – SilentGhost 2010-10-20 14:37:19

2

使用例外。

class Quit(Exception): pass 

running= True 
while running: 
    choice = main_menu() 

    if choice == "1": 
     os.system("clear") 

     try: 
      while True: 
       choice = app_menu() 

       if choice == "1": 

       elif choice == "2": 

       elif choice.lower() == "m": 
        break 
        # No statement after break is ever executed. 
       elif choice.lower() == "q": 
        raise Quit 
       sendfiles(source, target) 

     except Quit: 
      running= False 

    elif choice == "q": 
     running= False 
+0

這不工作。點擊第二個菜單中的「q」不會做任何事情。 – luckytaxi 2010-10-20 15:04:08

0

你可以把這個變成一個函數,返回:

import os.path 
def do_whatever(): 
    while True: 
     choice = main_menu() 

     if choice == "1": 
      os.system("clear") 

     while True: 
      choice = app_menu() 

      if choice in ("1", "2"): 
       app_version = app_version_10 if choice == "1" else app_version_8 
       source = os.path.join(app_help_path, app_version, "external") 
       target = os.path.join(target_app_help_path, app_version) 
       sendfiles(source, target) 
      elif choice.lower() == "m": 
       break 
      elif choice.lower() == "q": 
       return 

誠然,我不完全得到,當你想打破內部循環,當你想退出這兩個循環,但這會給你這個想法。