- 2

Dictionary function

can anyone help me what is wrong with this code while using get() function .the output should result "Book not found" when given number or name which is out of list..I'm getting none as output. #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() try: print(books.get(book)) except: print("Book not found")

10th Feb 2021, 2:15 PM
Dhanu Sri Mulakala
Dhanu Sri Mulakala - avatar
15 ответов
+ 6
**Lol looks like I accidentally sent this to a totally different thread but here was my answer** Because .get() function needs a default value as second parameter: get(key, alternative_value) By default, alternative value is None. And this won't raise a KeyError. There are 2 ways to solve this: 1. Remove the try-except and use the "Book not found' the default value. ______________________ print(books.get(book, "Book not found") ______________________ 2. Instead of using .get() function, just access the key so if key is not found, it will raise an error (KeyError) ______________________ try: print(books[book]) except: print("Book not found") ______________________
10th Feb 2021, 2:37 PM
noteve
noteve - avatar
+ 5
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("Book not found") here is the right one
26th Sep 2021, 10:05 PM
Fabrone BITITI
Fabrone BITITI - avatar
+ 3
The `dict.get()` method does NOT raise an error when the argument is not found in the dictionary. Instead, it returns the second argument or `None` if only a single argument is passed, when the key is not found in the dictionary. dict.get(): https://docs.python.org/3/library/stdtypes.html#dict.get I think you wanted to do `print(books[book])` which will raise an error when `book` is not found in the `books` dictionary, which will in turn, result in the 'except' block to be executed.
10th Feb 2021, 2:28 PM
XXX
XXX - avatar
+ 3
Dhanu Sri Mulakala You can use not in if book not in books: print ("Not found") else: print (books[book]) or you can do like this try: print (books[book]) except: print ("Not found")
10th Feb 2021, 2:36 PM
A͢J
A͢J - avatar
+ 3
Cyan I saw you gave that answer in other thread, and posted the thread's link here like a dork, thinking the OP confirmed with you through DM or whatever LMAO 😂😂😂
10th Feb 2021, 2:49 PM
Ipang
+ 2
Syntax: <dict>.get(<key>[, <default-value>]) `dict.get()` returns `None` when it can't find an item having specified key. Except when second optional argument <default-value> was given, in which case <default-value> be returned.
10th Feb 2021, 2:26 PM
Ipang
+ 2
Ipang 😅😂
10th Feb 2021, 2:52 PM
noteve
noteve - avatar
+ 1
try this if book in books: print(books[book]) else: print("Book not Found")
12th Feb 2021, 6:30 AM
Code_lover
Code_lover - avatar
+ 1
I think this would work Book=input() If (books.get(book)) == None: Print("not found") Else: Print(books.get(book))
7th Aug 2021, 7:14 AM
Vivek parab
Vivek parab - avatar
0
data = { 'Singapore': 1, 'Ireland': 6, 'United Kingdom': 7, 'Germany': 27, 'Armenia': 34, 'United States': 17, 'Canada': 9, 'Italy': 74 } user = input() print(data.get(user, "Not found"))
9th Nov 2021, 10:38 PM
Alejandro Romanazzi
Alejandro Romanazzi - avatar
0
the easiest, for me: print(books.get(book, "Not found"))
15th Nov 2021, 12:44 PM
Mar
Mar - avatar
0
Thunder Bird 16 If you write Hard Code logic then what is the use of books? It is not necessary that everytime you can have book name. If there is no book name is in test cases then you have to print "Not found". So use given dictionary books. See previous answer given by others.
29th Nov 2021, 4:44 AM
A͢J
A͢J - avatar
0
#easy one...Test case 2 and 5 gets right on this one. 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")
8th Jan 2022, 7:19 AM
Janvi Sharma
Janvi Sharma - avatar
0
# FULL SOLVED EFFICIENT ANSWER # After reading EVE'S comment, i added the second value in the get() and it worked 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 print(books.get(book, "Book not found"))
27th Feb 2022, 1:05 AM
Jake Ambrose
Jake Ambrose - avatar
- 1
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 == "Life of Pi": print("Adventure Fiction") if book == "The Three Musketeers": print("Historical Adventure") if book == "Watchmen": print("Comics") if book == "Bird Box": print("Horror") if book == "Harry Potter": print("Fantasy Fiction") if book == "Good Omens": print("Comedy") #i tried doing the last part im still stuck on it
29th Nov 2021, 4:00 AM
Fares Krichene
Fares Krichene - avatar