- 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")
16 ответов
+ 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")
______________________
+ 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
+ 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.
+ 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")
+ 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 😂😂😂
+ 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.
+ 2
Ipang 😅😂
+ 1
try this
if book in books:
print(books[book])
else:
print("Book not Found")
+ 1
I think this would work
Book=input()
If (books.get(book)) == None:
Print("not found")
Else:
Print(books.get(book))
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"))
0
the easiest, for me:
print(books.get(book, "Not found"))
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.
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")
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"))
- 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