【问题标题】:Search inventory and display information搜索库存并显示信息
【发布时间】:2019-03-01 13:20:55
【问题描述】:

我正在尝试编写一个搜索数据对象列表的代码,如果用户没有输入参考号,它应该在用户输入有效的参考号时打印“没有找到这样的项目”,它应该显示信息关于那个项目。我的代码现在的方式是,它不会为每个数据对象打印出这样的项目,而不仅仅是一次。如何让它只打印一次?

def initialize():
    medialist=[
    MediaItem("TU2RL012","Movie","2001: A Space Odyssey",11.99, None ,"Stanley Kubrick","Keir Dullea"),
    MediaItem("GV5N32M9","Book","A Brief History of Time",10.17,"Stephen Hawking", None, None),
    MediaItem("1DB6HK3L","Movie","North by Northwest",8.99, None, "Alfred Hitchcock","Cary Grant"),
    MediaItem("PO5T7Y89","Movie", "The Good, The Bad, The Ugly",9.99,None,"Sergio Leone", "Clint Eastwood"),
    MediaItem("TR3FL0EW","Book","The Alchemist",6.99,"Paulo Coelho", None,None),
    MediaItem("F2O9PIE9", "Book", "Thus Spoke Zarathustra",7.81, "Friedrich Nietzsche", None, None),
    MediaItem("R399CED1","Book", "Jonathan Living Seagull",6.97,"Richard Bach", None, None),
    MediaItem("2FG6B2N9","Movie", "Gone with the Wind",4.99, "Victor Fleming","Vivien Leigh", None),
    MediaItem("6Y9OPL87","Book", "Gone with the Wind",7.99, "Margarett Mitchell", None, None)]
    return medialist

def search_item():
    referencenum=input("Enter item reference:")
    for obj in initialize():
        if referencenum != obj.reference:
            print("No Such Object found")`

【问题讨论】:

  • 您尝试过什么来调试您的问题?如果只打印一次而不是在该循环的每次迭代中打印呢?

标签: python inventory


【解决方案1】:

您可以先将所有对象参考编号收集到一个列表中

obj_references = [obj.reference for obj in initialize()]

然后检查是否匹配:

def search_item():
    referencenum=input("Enter item reference:")
    obj_references = [obj.reference for obj in initialize()]
    if referencenum not in obj_references:
        print("No Such Object found")

要显示有关对象的更多信息,您可以先收集所有对象

objects = initialize()

然后,分别收集你需要的关于每个对象的信息

obj_references = [obj.reference for obj in objects]
obj_titles = [obj.title for obj in objects]
obj_ratings = [obj.rating for obj in objects]
obj_authors = [obj.author for obj in objects]

请注意obj.titleobj.ratingobj.author 是如何提取对象属性的示例。我不知道确切的属性名称,因此您需要将 titleratingauthor 替换为正确的名称。

然后,检查用户输入的参考号,如果匹配则打印信息

if referencenum not in obj_references:
    print("No Such Object found")
else:
    obj_id = obj_references.index(referencenum)
    print("Title:", obj_titles[obj_id], "Rating:", obj_ratings[obj_id],               
          "Author:", obj_authors[obj_id])

obj_id = obj_references.index(referencenum) 返回输入参考编号的索引,使用该索引您可以从它们各自的列表中提取标题、评级和作者(例如obj_titles[obj_id])。

完整的函数如下所示:

def search_item():
    referencenum = input("Enter item reference:")

    objects = initialize()
    obj_references = [obj.reference for obj in objects]
    obj_titles = [obj.title for obj in objects]
    obj_ratings = [obj.rating for obj in objects]
    obj_authors = [obj.author for obj in objects]

    if referencenum not in obj_references:
        print("No Such Object found")
    else:
        obj_id = obj_references.index(referencenum)
        print("Title:", obj_titles[obj_id], "Rating:", obj_ratings[obj_id],               
              "Author:", obj_authors[obj_id])

【讨论】:

  • 如果用户输入奖牌获得者的参考号,我怎么能做到这一点,它会显示有关该对象的信息?例如,如果我输入 R399CED1,它将打印 "Jonathan Living Seagull",6.97,"Richard Bach"
【解决方案2】:

您可以使用列表解析并使用not in,而不是单独对所有对象进行for 循环。

def search_item():
    reference_num = input("Enter item reference:")
    if reference_num not in [obj.reference for obj in initialize()]:
        print('No such object found')   

【讨论】:

  • 如果用户输入奖牌获得者的参考号,我怎么能做到这一点,它会显示有关该对象的信息?例如,如果我输入 R399CED1,它将打印 "Jonathan Living Seagull",6.97,"Richard Bach"
  • 只需在 if 语句后添加 else 并打印 obj 的字段。您希望在 if 语句之前保存从 initialize() 返回的列表,因为现在您将多次使用该列表。
猜你喜欢
  • 2012-12-21
  • 1970-01-01
  • 1970-01-01
  • 2012-05-29
  • 2019-07-18
  • 2019-12-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多