通过使用python获取二维数组中的前两个元素来创建用户菜单

通过使用python获取二维数组中的前两个元素来创建用户菜单,python,Python,我是python编程新手,我花了3天多的时间试图编写这一部分的代码,真的让我抓狂。如果有人能帮我编码,我将不胜感激 我必须用下面的二维数组创建一个子菜单选择。菜单必须采用以下格式: 苹果MacBookAir 宏碁Aspire SW5-111 华硕记事本7 三星标签S8.4 苹果iphone7 三星Galaxy S8 输入M返回主菜单 否则请输入您的选择 代码 编辑:Python代码: import sys product_list = [ ["Mac Book Air","Apple"

我是python编程新手,我花了3天多的时间试图编写这一部分的代码,真的让我抓狂。如果有人能帮我编码,我将不胜感激

我必须用下面的二维数组创建一个子菜单选择。菜单必须采用以下格式:

  • 苹果MacBookAir
  • 宏碁Aspire SW5-111
  • 华硕记事本7
  • 三星标签S8.4
  • 苹果iphone7
  • 三星Galaxy S8
  • 输入M返回主菜单 否则请输入您的选择

    代码

    编辑:Python代码:

    import sys
    
    product_list = [
        ["Mac Book Air","Apple","Laptop","Equipped with the new fifth-generation Intel Core i5 and i7 processors with Intel HD Graphics.",1350.5],
        ["Aspire SW5-111", "Acer", "Laptop", "Intel® Atom Z3745 processor Quad-core 1.33 GHz CPU.", 510],
        ["MeMo Pad 7", "Asus", "Tablet", "The 7 inch ASUS MeMO Pad 7 was created for those looking for a value tablet.", 620],
        ["Tab S8.4", "Samsung", "Tablet", "You will be surprised by the slim and sleek design of the GALAXY Tab S (8.4\") LTE. It only weighs 298g (LTE) and is easy to carry anywhere.", 788.5],
        ["iPhone 7", "Apple", "Phone", "The iPhone 7 is an exceptional phone in nearly every way.", 900],
        ["Galaxy S8", "Samsung", "Phone", "Newly released!", 1000]
    ]
    
    # Loop until user says "exit"
    while True:
        # Print list of options
        for product_index in range(len(product_list)):
            print("%x: %s %s" % (product_index, product_list[product_index][1], product_list[product_index][0]))
    
        # Read keyboard input from user
        chosen_index = input("Select option: ")
    
        # Check if input is a number
        try:
            # It is a number, do whatever you want to do
            chosen_index = int(chosen_index)
            print("Info: %s\n" % product_list[chosen_index][3])
        except ValueError:
            # It's not a number, is the exit command?
            if chosen_index.lower() == "exit":
                print("Exiting..")
                sys.exit()
    
            # It's not the exit command, give an error message
            print("ERROR: Invalid option. Please provide a number\n")
            continue
    
    python中的
    enumerate()
    函数返回一个元组列表,其中包含项索引和列表中的项。i、 e.上面的枚举函数返回:

    [(0,["Mac Book Air".....])
    
    …一直到:

    (5,["Galaxy s8",...1000])]
    

    格式化代码段我尝试运行代码,但遇到一个错误,即原始输入未定义。我可以知道我应该如何定义这个吗?请您也提供代码,以便根据选择列出项目,好吗?例如,如果用户从选项中选择“1”,系统将相应地按名称:、品牌:、类别:、说明:价格列出详细信息(根据产品列表)。您可能正在使用python 3,此代码是用python 2编写的。只需将
    raw\u input()
    更改为
    input()
    ,并将print语句括在括号中,即将它所说的
    print“something”
    更改为
    print(“something”)
    。我将把剩余信息的打印留给您去弄清楚,这是一个很好的做法:)@j.koo查看我对python3实现的更新答案。顺便问一下,如果我想从1而不是0启动菜单,我应该如何引用它?只需在打印函数中的
    产品索引
    中添加1,就像这样:
    打印(%x:%s%s)%(product_index+1,product_list[product_index][1],product_list[product_index][0])
    。请注意,实际索引不会更改,因此用户在想要访问索引0时会键入1,因此您需要从所选索引中减去1:
    Selected_index=int(Selected_index)-1
    。顺便说一句,如果我的答案能回答你的问题,请接受。
    # Python 3
    
    def main_menu():
        listOfProducts = [["Mac Book Air","Apple","Laptop","Equipped with the new fifth-generation Intel Core i5 and i7 processors with Intel HD Graphics.",1350.5],
            ["Aspire SW5-111", "Acer", "Laptop", "Intel® Atom Z3745 processor Quad-core 1.33 GHz CPU.", 510],
            ["MeMo Pad 7", "Asus", "Tablet", "The 7 inch ASUS MeMO Pad 7 was created for those looking for a value tablet.", 620],
            ["Tab S8.4", "Samsung", "Tablet", "You will be surprised by the slim and sleek design of the GALAXY Tab S (8.4\") LTE. It only weighs 298g (LTE) and is easy to carry anywhere.", 788.5],
            ["iPhone 7", "Apple", "Phone", "The iPhone 7 is an exceptional phone in nearly every way.", 900],
            ["Galaxy S8", "Samsung", "Phone", "Newly released!", 1000]]
    
        nr = 1
        for s in listOfProducts:
            print('%d. %s %s' % (nr, s[1], s[0]))
            nr += 1
        return input('\nEnter M to return to Main Menu Else please enter your selection: ')
    
    print(main_menu())
    
    for i, product in enumerate(listOfProducts):
        print('%s. %s %s' % (i+1, product[1], product[0]))
    
    [(0,["Mac Book Air".....])
    
    (5,["Galaxy s8",...1000])]