簡體   English   中英

不會打印項目列表,只打印列表中的第一項

[英]Will not print a list of items, only the first item on the list

所以對於我的項目,我正在創建一個可以存放書籍和電影的媒體庫,並且已經存放了一些庫存。我正在為每個項目制作一個列表(因為作業說要對每個項目進行硬編碼,因為我們還沒有學習了另一種方式,並且列表仍然只打印關於空間或其他東西的書的第一項。如果需要其他文件運行,請告訴我,涉及三個文件。

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.
    """

在您的代碼中,您有

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])此外,您還需要為每個項目添加該行。 在您當前的代碼中,您只執行一次。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM