我如何打印列?如何仅打印10到20之间的项目?

问题描述 投票:0回答:1

我正在研究使用嵌套列表显示菜单的代码,但所有文本均保持左对齐。在菜单列表中,我需要创建一个嵌套列表,其中包含100

然后索引列占用4个空间,食品列占20个空间,卡路里列占15个空间,碳水化合物列占15个空间。

有人可以告诉我我的代码有什么问题吗?帮我做?

这应该是我的输出enter image description here

python
1个回答
0
投票

这是我到目前为止所得到的

import csv

def menu_list(filep):
    """Function to read the csv file, create a nested list and return the list that is sorted based on calories in the ascending order."""

    menulist = []
    with open(filep) as csv_file:
        csv_reader = csv.reader (csv_file, delimiter=',')
        for row in csv_reader:
            mlist = []
            for i in range(len(row)):
                mlist.append(row[i])
            menulist.append(mlist)
    menulist.sort(key=lambda x: x[1])
    return menulist

def show_menu(menulist):
    print ('Food Calories Carbs')
    #Function to display menu
    for i in range(len(menulist)):
        print ('%-4d%-20s%-15s%-15s' % (i + 1, menulist[i][0], menulist[i][2], menulist[i][1]))

def make_order():
    #Function make an order
    print('*** Order foods from the following menu ***')
    menulist = menu_list('food.csv')
    show_menu(menulist)
    n = len(menulist)
    order_menu = []

    while True:
        try:
            s = input('Input food number: (q or Q to quit) ')
            if s == 'q' or s =='Q':
                break
            x = int(s) - 1
            if x > n or x < 0:
                print ('Please select a food that is in the menulist.')
            elif order_menu.__contains__(menulist[x]):
                print('It is already selected. Please select others.')
            else: 
                order_menu.append(menulist[x])
        except ValueError:
             print('Please input the food number.')


    print("Here is your order: ")
    show_menu(order_menu)

make_order()
© www.soinside.com 2019 - 2024. All rights reserved.