2013-01-11 58 views
1

我正在嘗試創建子菜單的主菜單。我想這樣做,無論用戶從主菜單選擇子菜單的順序如何,他們最終都會被引導回主菜單 - 也就是說,直到他們選擇最後一個選項(3.保持並結束轉動) 。如何循環有python子菜單的菜單?

這是我陷入困境。目前,用戶只能瀏覽一次菜單序列。我如何使這個重複循環?

#main menu 
playermenumain=raw_input("What would you like to do?\n (1) Buy\n (2)Sell\n (3)Hold and end turn ") 

if playermenumain=="1": 

     buyermenu=raw_input("In what area would you like to buy?"\n (1)Stocks\n (2) Bonds\ (3) Nevermind\n) 
     if buyermenu=="1": 
      stockamount=stockamount+500 
     elif buyermenu=="2": 
      bondamount=bondamount+500 
     else: 
      buyermenu=raw_input("In what area would you like to buy?"\n (1)Stocks\n (2) Bonds\ (3) Nevermind\n) 

if playermenumain=="2": 

    sellermenu=raw_input("In what area would you like to buy?"\n (1)Stocks\n (2) Bonds\ (3) Nevermind\n) 
     if sellermenu=="1": 
      stockamount=stockamount-500 
     elif sellermenu=="2": 
      bondamount=bondamount-500 
     else: 
      sellermenu=raw_input("In what area would you like to buy?"\n (1)Stocks\n (2) Bonds\ (3) Nevermind\n) 

if playermenumain=="3": 

    break 
#main menu loop: no matter what is selected, player is directed back to main menu until option 3 (end turn) is selected: 

while playermenumain=="1" or playermenumain=="2": 

     playermenumain=raw_input("What would you like to do?\n (1) Buy\n (2)Sell\n (3)Hold and end turn ") 
     continue 
+0

這是怎麼回事的參數'raw_input'?你顯示的內容看起來不像有效的語法。 –

回答

1

嘗試使用while循環而不是while條件。例如:

def submenu_buy(): 
    # Put your submenus here 
    pass 

def submenu_sell(): 
    # Put your submenus here 
    pass 

while True: 
    playermenumain=raw_input("What would you like to do?\n (1) Buy\n (2)Sell\n (3)Hold and end turn ") 
    if playermenumain == "1": 
    submenu_buy() 
    elif playermenumain == "2": 
    submenu_sell() 
    elif playermenumain == "3": 
    break 
+0

謝謝...這很有幫助! – user1959916

0

您應該將主菜單代碼放在方法中,然後在循環中調用該方法。每個子菜單也應該是一個方法調用,並且應該在返回到上一個菜單時返回。

您應該查看基本的方法使用和程序流程;這比一個特定的語言問題更像是一個通用的編程概念。幾乎所有你會做的事情都很重要,所以你應該花時間去真正理解它。

0

我有幾個建議:

  1. 創建一個功能菜單()向)顯示菜單,b)駁回無效的響應,以及c)返回一個有效的響應。這個菜單將消除你的代碼中的冗餘,因爲你需要它3次。
  2. 移動銷售邏輯到一個單獨的功能,讓我們叫它賣()
  3. 同樣,移動購買邏輯買()
  4. 使用下劃線讓你標識符(變量和函數名)更容易閱讀。我更喜歡 'playermenumain'

這些建議 'main_menu_response',我的實現是:

def menu(prompt, choices): 
    print '\n\n{0}\n'.format(prompt) 
    count = len(choices) 
    for i in range(count): 
     print '({0}) {1}'.format(i + 1, choices[i]) 
    response = 0 
    while response < 1 or response > count: 
     response = raw_input(' Type a number (1-{0}): '.format(count)) 
     if response.isdigit(): 
      response = int(response) 
     else: 
      response = 0 
    return response 

def buy(stockamount, bondamount): 
    response = menu('What to buy?', ['Stocks', 'Bonds', 'Nevermind']) 
    # Do something 

def sell(stockamount, bondamount): 
    response = menu('What to sell?', ['Stocks', 'Bonds', 'Nevermind']) 
    # Do something 

# ====================================================================== 
# Main program starts here 
# ====================================================================== 
stockamount=10000 
bondamount=10000 

main_menu_response = 0 
while main_menu_response != 3: 
    main_menu_response = menu('What to do?', ['Buy', 'Sell', 'End']) 
    if main_menu_response == 1: 
     buy(stockamount, bondamount) 
    elif main_menu_response == 2: 
     sell(stockamount, bondamount) 
+0

謝謝! – user1959916