我正在尝试编写一个搜索数据对象列表的代码,如果用户没有输入参考号,当用户输入有效的参考号时,它应该打印“没有找到这样的项目”,它应该显示有关该项目的信息. 我的代码现在的方式是,它不会为每个数据对象打印没有这样的项目,而不仅仅是一次。我怎样才能让它只打印一次?
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")`
心有法竹
三国纷争
相关分类