将不打印项目列表,仅打印列表中的第一项

时间:2019-06-17 20:35:05

标签: python python-3.x

因此,对于我的项目,我正在创建一个可以容纳书籍和电影的媒体库,并且已经有库存。我为每个项目都列出了清单(因为作业要求对每个项目进行硬编码,因为我们还没有学会另一种方法,列表仍然只打印关于空间或某物的第一本书。如果需要其他文件来运行,请涉及三个文件。

from MediaItem import MediaItem


def initialize():
    """Declares the list all_items and adds
    the initial MediaItem objects.
    Note that these data would come from a database in real-world
    applications. The data would then be represented in the program
    as MediaItem objects as below.
    """
    all_items = []
    # item 1
    item = MediaItem()
    item.media = "Movie"
    item.title = "2001: A Space Odyssey"
    item.price = 11.99
    item.ref = "TU2RL012"
    item.director = "Stanley Kubrick"
    item.lead_actor = "Keir Dullea"
    all_items = all_items + [item]
    # item 2
    item = MediaItem()
    item.media = "Book"
    item.title = "A Brief History of Time"
    item.price = 10.17
    item.ref = "GV5N32M9"
    item.author = "Stephen Hawking"
    # item 3
    item = MediaItem()
    item.media = "Movie"
    item.title = "North by Northwest"
    item.price = 8.99
    item.director = "Alfred Hitchcock"
    item.lead_actor = "Cary Grant"
    return all_items


def display_menu():
    """Prints the menu of options.
    No parameters, no return.
    """
    print("\nMenu");
    print("====");
    print("1-List Inventory");
    print("2-Info Inventory");
    print("3-List of All Books");
    print("4-List of All Movies");
    print("5-Item Description");
    print("6-Remove Item");
    print("7-Add Item");
    print("8-Set Maximum Price");
    print("0-Exit\n");


######## Implement all other functions listed below

def display(all_items, media="all"):
    """Prints all of the data for the MediaItems on the
    all_items list passed in. The parameter media is used
    to select for only "Book", "Movie", or, by default, "all".
    """
    print("Reference / Media / Title /\n")
    print("-----------------------------")
    for item in all_items:
        if media == "Book" and item.media == "Book":
            print(item.ref, "\t", item.media, "\t", item.title, "\n")

        if media == "Movie" and item.media == "Movie":
            print(item.ref, "\t", item.media, "\t", item.title, "\n")

        if media == "all":
            print(item.ref, "\t", item.media, "\t", item.title, "\n")


def info(all_items):
    """Calculates and prints a report of
    the total value of items in the all_items list passed in,
    the most expensive item, and the total number of each media type.
    """


def search_item(all_items, target_ref):
    """Searches the list of items in the all_items list passed in
    for a match on the reference field, target_ref.
    Returns the MediaItem object if a match is found, otherwise it
    returns None.
    """


def display_item(item):
    """Prints all of the data in the MediaItem object, item, passed in.
    """


def search_item_index(all_items, target_ref):
    """Searches the list all_items for a match on the reference
    field target_ref. Returns the index of the item that matches the target_ref,
    returns None if no match was found in the all_items.
    The index is zero-based.
    """


def create_item(media_type):
    """Creates a new MediaItem object and returns it.
    The argument media_type is either the string "Book" or "Movie".
    The function prompts the user for the data required for
    the type of media specified by the parameter media_type.
    """

1 个答案:

答案 0 :(得分:0)

您的代码中有

item = MediaItem()
    item.media = "Movie"
    item.title = "2001: A Space Odyssey"
    item.price = 11.99
    item.ref = "TU2RL012"
    item.director = "Stanley Kubrick"
    item.lead_actor = "Keir Dullea"
    all_items = all_items + [item]

尝试将all_items = all_items + [item]替换为all_items.append([item]) 另外,您需要为每个项目添加该行。在当前代码中,您只需执行一次。