1

我正在尝试创建带有子菜单的主菜单。我想这样做,无论用户从主菜单到子菜单选择什么顺序,他们最终都会被引导回主菜单——也就是说,直到他们选择最后一个选项(3.按住并结束转弯) .

这就是我卡住的地方。现在,用户只能通过菜单序列一次。我如何使它成为一个重复循环?

#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
4

3 回答 3

1

尝试使用带有 break 的 while True 循环,而不是您拥有的 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
于 2013-01-11T22:06:22.793 回答
0

您应该将主菜单代码放在一个方法中,并在循环中调用该方法。每个子菜单也应该是一个方法调用,并且应该在您返回上一个菜单时返回。

您应该查看基本的方法使用和程序流程;这更像是一个通用的编程概念,而不是一个特定的语言问题。它对您将要做的几乎所有事情都至关重要,因此您应该花时间真正理解它。

于 2013-01-11T22:07:48.743 回答
0

我有几个建议:

  1. 创建一个函数 menu() 以 a) 显示菜单,b) 拒绝无效响应,以及 c) 返回有效响应。此菜单将删除代码中的冗余,因为您需要它 3 次。
  2. 将销售逻辑移到一个单独的函数中,我们称之为 sell()
  3. 同样,将购买逻辑移至 buy()
  4. 使用下划线使标识符(变量和函数名)更易于阅读。我更喜欢“main_menu_response”而不是“playermenumain”

有了这些建议,我的实现是:

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)
于 2013-01-11T23:02:25.380 回答