Trying to solve library management in python core
Know am missing something but can seem to remember books = { "Life of Pi": "Adventure Fiction", "The Three Musketeers": "Historical Adventure", "Watchmen": "Comics", "Bird Box": "Horror", "Harry Potter":"Fantasy Fiction", "Good Omens": "Comedy" } book = input() #your code goes here if book not in books: print("Not found")
4/8/2021 12:46:10 PM
Simisola Osinowo
14 Answers
New Answerit works like this : books = { "Life of Pi": "Adventure Fiction", "The Three Musketeers": "Historical Adventure", "Watchmen": "Comics", "Bird Box": "Horror", "Harry Potter":"Fantasy Fiction", "Good Omens": "Comedy" } book = input() if book in books: print(books[book]) #your code goes here else :
books = { "Life of Pi": "Adventure Fiction", "The Three Musketeers": "Historical Adventure", "Watchmen": "Comics", "Bird Box": "Horror", "Harry Potter":"Fantasy Fiction", "Good Omens": "Comedy" } book = input() #your code goes here print (books.get(book, "Not found"))
This worked for me(: books = { "Life of Pi": "Adventure Fiction", "The Three Musketeers": "Historical Adventure", "Watchmen": "Comics", "Bird Box": "Horror", "Harry Potter":"Fantasy Fiction", "Good Omens": "Comedy" } book = input() #change this part to use the .get() method if book in books: print(books.get(book)) else: print("Not found")
i think since its a dictionary, you could compare input to the dictionary keys instead of the key:value pairs. if found, you'd need to access the dictionary by using the key and print out its genre value.
#add the print statement in else part books = { "Life of Pi": "Adventure Fiction", "The Three Musketeers": "Historical Adventure", "Watchmen": "Comics", "Bird Box": "Horror", "Harry Potter":"Fantasy Fiction", "Good Omens": "Comedy" } book = input() if book in books: print(books[book]) else: print("Not found")
# Most efficient simplest full answer books = { "Life of Pi": "Adventure Fiction", "The Three Musketeers": "Historical Adventure", "Watchmen": "Comics", "Bird Box": "Horror", "Harry Potter":"Fantasy Fiction", "Good Omens": "Comedy" } book = input() if book in books: print(books[book]) else: print("Not found")
A library management program has a dictionary of books with their corresponding genres. Take a book name as input and output the genre.
When the problem is asking to use the .get() method instead of if/else, I used: print(books.get(book, "Book not found"))
books = { "Life of Pi": "Adventure Fiction", "The Three Musketeers": "Historical Adventure", "Watchmen": "Comics", "Bird Box": "Horror", "Harry Potter":"Fantasy Fiction", "Good Omens": "Comedy" } book = input() print(books.get(book, "Not found"))