I am trying to write a code that searches a list of data objects, if the user doesn't enter a reference number it should print "no such item found" when the user enters a valid reference number, it should display information about that item. The way my code is now, it prints no such item found for every data object instead of just once. How can I make it only print once?
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")`
You could first collect all object reference numbers in a list
obj_references = [obj.reference for obj in initialize()]
and then check for a match:
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")
To display more information about an object you could first collect all the objects
objects = initialize()
Then, separately collect the information you need about each object
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]
Please note that obj.title
, obj.rating
and obj.author
are the examples of how to extract the object properties. I don't know the exact properties names, so you would need to replace title, rating and author with the correct names.
Then, check the reference number entered by a user and in case of a match print the information
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)
returns the index of the entered reference number and using this index you can extract the title, rating and author from their respective lists (e.g. obj_titles[obj_id]
).
The complete function would look like this:
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])