为了账号安全,请及时绑定邮箱和手机立即绑定

搜索库存和显示信息

搜索库存和显示信息

天涯尽头无女友 2021-11-23 16:18:40
我正在尝试编写一个搜索数据对象列表的代码,如果用户没有输入参考号,当用户输入有效的参考号时,它应该打印“没有找到这样的项目”,它应该显示有关该项目的信息. 我的代码现在的方式是,它不会为每个数据对象打印没有这样的项目,而不仅仅是一次。我怎样才能让它只打印一次?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 medialistdef search_item():    referencenum=input("Enter item reference:")    for obj in initialize():        if referencenum != obj.reference:            print("No Such Object found")`
查看完整描述

2 回答

?
心有法竹

TA贡献1866条经验 获得超5个赞

您可以首先收集列表中的所有对象引用号


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.title,obj.rating和obj.author是如何提取对象属性的示例。我不知道确切的属性名称,因此您需要用正确的名称替换title、rating和author。


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


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])


查看完整回答
反对 回复 2021-11-23
?
三国纷争

TA贡献1804条经验 获得超7个赞

您可以使用列表理解并使用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') 


查看完整回答
反对 回复 2021-11-23
  • 2 回答
  • 0 关注
  • 195 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信