【问题标题】:Will not print a list of items, only the first item on the list不会打印项目列表,只打印列表中的第一项
【发布时间】:2019-06-17 20:35:05
【问题描述】:

因此,对于我的项目,我正在创建一个可以容纳书籍和电影的媒体库,并且已经有库存。我正在为每个项目制作一个列表(因为作业要求对每个项目进行硬编码,因为我们还没有学会别的方法,而且列表还是只打印了关于空间什么的书的第一项。涉及三个文件,如果需要其他的运行看看让我知道。

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

【问题讨论】:

  • 所以问题是只打印第一个元素?您只是将第一个元素添加到列表中。在创建每个项目后,您需要 all_items = all_items + [item] 行。
  • 啊,我明白了,谢谢解决问题的人。
  • 使用@Ic74 提到的追加方法。您当前的方法每次添加时都会创建一个新列表,append 会添加到现有列表中。
  • 考虑将数据放入MediaItem__init__ 函数中。像这样的东西:MediaItem('Movie', 'North by Northwest') 等等。

标签: python python-3.x


【解决方案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]

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

【讨论】:

  • all_items.append([item]) 创建一个 1-element-lists 的列表,它是一个 MediaItem ....
  • @PatrickArtner 我没有运行任何代码,但我认为这就是原始帖子的目的。但如果不是,您可以删除项目周围的方括号。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2017-10-28
  • 2020-06-13
  • 2022-08-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多