+ 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")
15 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")
0
class Book:
def __init__(self, title, author):
self.title = title
self.author = author
class Ebook(Book):
def __init__(self, title, author, file_size):
super().__init__(title, author) # Call the parent class constructor
self.file_size = file_size # Initialize the file_size attribute
# Creating an instance of Ebook
my_ebook = Ebook("1984", "George Orwell", 10)
print(my_ebook.title) # Output: 1984
print(my_ebook.author) # Output: George Orwell
print(my_ebook.file_size, 'MB') # Output: 10 MB
- 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"))