Advertisement
Nickel59

Untitled

Dec 9th, 2022
31
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.52 KB | Source Code | 0 0
  1. class Book:
  2.     def __init__(self, author: str, name: str, publisher: str, year: int, page_num: int):
  3.         self.author = author
  4.         self.name = name
  5.         self.publisher = publisher
  6.         self.year = year
  7.         self.page_num = page_num
  8.  
  9.     def __str__(self):
  10.         return f'{self.author}, {self.name}, {self.publisher}, {self.year}, {self.page_num}'
  11.  
  12.  
  13. # Неважливо який ліст, головне щоб знизу була коректна логіка для їх фільтрування
  14. book_list = [Book('TNU', 'A VERY INTERSTING BOOK', 'A RANDOM GUY', 1932, 100),
  15.              Book('TNU', 'ANOTHER INTERISTING BOOK', 'SUBURBIAN CITIZENS', 1999, 50)]
  16.  
  17. inp = input('How would you like to filter your books?\n'
  18.             'Type 1 to filter them by author.\n'
  19.             'Type 2 to filter them by publisher.\n'
  20.             'Type 3 to filter to them by date.\n')
  21.  
  22. result = []
  23. match inp:
  24.     case '1':
  25.         author = input('Type the author name >>> ')
  26.         result = [book for book in book_list if book.author == author]
  27.  
  28.     case '2':
  29.         publisher = input('Type the publisher name >>> ')
  30.         result = [book for book in book_list if book.publisher == publisher]
  31.     case '3':
  32.         year = ''
  33.         while not year.isdigit():
  34.             year = input('Type the year after which to show the books >>> ')
  35.         year = int(year)
  36.         result = [book for book in book_list if book.year > year]
  37.  
  38. if result:
  39.     print(*result, sep='\n')
  40. else:
  41.     print('Found nothing')
  42.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement