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

2024-10-02 18:22:51 发布

您现在位置:Python中文网/ 问答频道 /正文

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

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

  1. 苹果Mac Book Air
  2. 宏碁Aspire SW5-111
  3. 华硕记事本7
  4. 三星Tab S8.4
  5. 苹果iPhone 7
  6. 三星Galaxy S8

输入M返回主菜单 否则请输入您的选择

代码

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]]

Tags: andthe代码苹果mac菜单airtab
3条回答
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 = raw_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

编辑:Python3代码:

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 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]))

python中的enumerate()函数返回一个元组列表,其中包含项索引和列表中的项。i、 e.上面的枚举函数返回:

[(0,["Mac Book Air".....])

…一直到:

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

相关问题 更多 >