¡Nuevo curso! ¡Todo programador debería aprender IA Generativa!
Prueba una lección gratuita+ 1
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")
14 Respuestas
+ 1
it 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 :
+ 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
print (books.get(book, "Not found"))
+ 1
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")
0
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.
0
Thanks
0
if book in books:
print(books[book])
0
book = input()
try:
print(books[book])
except:
print("Not found")
0
#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")
0
# 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")
- 1
A library management program has a dictionary of books with their corresponding genres.
Take a book name as input and output the genre.
- 1
This might help:
print(books.get(input(), "Not found"))
- 1
for i in books:
if i==book:
print(books[i])
break
else:
print("Not found")
- 1
When the problem is asking to use the .get() method instead of if/else, I used:
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()
print(books.get(book, "Not found"))