第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問(wèn)題,去搜搜看,總會(huì)有你想問(wèn)的

搜索庫(kù)存和顯示信息

搜索庫(kù)存和顯示信息

天涯盡頭無(wú)女友 2021-11-23 16:18:40
我正在嘗試編寫一個(gè)搜索數(shù)據(jù)對(duì)象列表的代碼,如果用戶沒(méi)有輸入?yún)⒖继?hào),當(dāng)用戶輸入有效的參考號(hào)時(shí),它應(yīng)該打印“沒(méi)有找到這樣的項(xiàng)目”,它應(yīng)該顯示有關(guān)該項(xiàng)目的信息. 我的代碼現(xiàn)在的方式是,它不會(huì)為每個(gè)數(shù)據(jù)對(duì)象打印沒(méi)有這樣的項(xiàng)目,而不僅僅是一次。我怎樣才能讓它只打印一次?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貢獻(xiàn)1866條經(jīng)驗(yàn) 獲得超5個(gè)贊

您可以首先收集列表中的所有對(duì)象引用號(hào)


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

要顯示有關(guān)對(duì)象的更多信息,您可以先收集所有對(duì)象


objects = initialize()

然后,分別收集你需要的關(guān)于每個(gè)對(duì)象的信息


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]

請(qǐng)注意obj.title,obj.rating和obj.author是如何提取對(duì)象屬性的示例。我不知道確切的屬性名稱,因此您需要用正確的名稱替換title、rating和author。


然后,檢查用戶輸入的參考編號(hào),如果匹配,則打印信息


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)返回輸入的參考編號(hào)的索引,使用該索引,您可以從各自的列表中提取標(biāo)題、評(píng)級(jí)和作者(例如obj_titles[obj_id])。


完整的函數(shù)如下所示:


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


查看完整回答
反對(duì) 回復(fù) 2021-11-23
?
三國(guó)紛爭(zhēng)

TA貢獻(xiàn)1804條經(jīng)驗(yàn) 獲得超7個(gè)贊

您可以使用列表理解并使用not in而不是for單獨(dú)遍歷所有對(duì)象。


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


查看完整回答
反對(duì) 回復(fù) 2021-11-23
  • 2 回答
  • 0 關(guān)注
  • 232 瀏覽
慕課專欄
更多

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購(gòu)課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)