Can help to explain the steps for these? | Sololearn: Learn to code for FREE!

+4

Can help to explain the steps for these?

So basically theres this question involving lessons on dictionary but I'm very confused on how to use the .get function, so can anyone help to explain how to use the function in this case. This is the initial code: 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[book]) else: print("Book not found") Rewrite the given code to use the .get() method instead of the if/else statements. Also, change the output to "Book not found", when the book is not found.

4/14/2021 4:02:06 AM

Zhi Hong

4 Answers

New Answer

+9

books.get(book,"Book not found")

+5

Return book and in default if book don't exist return Book not found

0

Илья Мирошник ohhh was thinking of that but got confused thanks for clearing it up for me appreciated it man!

0

print (books.get(book,"Book not found")) #if book in books => print original value #if book not in books => creat a new key(book):value