简体   繁体   中英

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. In your current code you are only doing it once.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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