简体   繁体   English

不会打印项目列表,只打印列表中的第一项

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

so for my project I am creating a media library that can hold books and movies, and there is a stock already held in. I am making a list for each item (because the assignment says to hard code every single one because we haven't learned another way yet, and the list is still only printing the first item the book about space or something. There are three files involved if the other ones are needed to run and see let me know.所以对于我的项目,我正在创建一个可以存放书籍和电影的媒体库,并且已经存放了一些库存。我正在为每个项目制作一个列表(因为作业说要对每个项目进行硬编码,因为我们还没有学习了另一种方式,并且列表仍然只打印关于空间或其他东西的书的第一项。如果需要其他文件运行,请告诉我,涉及三个文件。

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

In your code you have在您的代码中,您有

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]

try replacing all_items = all_items + [item] with all_items.append([item]) Also you need to add that line for each item.尝试将all_items = all_items + [item]替换为all_items.append([item])此外,您还需要为每个项目添加该行。 In your current code you are only doing it once.在您当前的代码中,您只执行一次。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM