Basically I have a dictionary for books and the genre its in. I am letting the user input the book and it output the genre. I need to make it so it outputs None
as 'Book not found'. I found a solution but I believe it's not the right way to go about targeting my problem. Could I have some guidance on how I can go and make it so None
outputs '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",
None: 'Book not found'
}
book = input()
#change this part to use the .get() method
if book in books:
print(books.get(book))
else:
print('Book not found')
CodePudding user response:
get
takes a second argument to return if the key is not present
print(books.get(book, 'Book not found'))
CodePudding user response:
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, 'Book not found'))