American Young Coder

AYC logo
Loading Events

« All Events

  • This event has passed.

6:00 pm- Python OOP – Joshua

July 17 @ 6:00 pm - 7:00 pm

Today we did

  • reviewed dictionaries
  • Reviewed how to use dictionaries in our classes
  • reviewed for loops in dictionaries

 

Homework

  • In our Main file, we have 4 goals for homework. Chloe and Justin, look at the bottom to put our work in progress code

PART 1

  • in our add_books() function, i want you to add one more line of code
  • to our dictionary (self.books_by_id), i want you to add a new key:value pairing
    • the “key” should be our id
    • the “value” should be our book information (“stored in the book we created with the inputs)

PART 2

  • Add a new Class function  self.list_all_books(self)
  • the Class should go through our dictionary, and then print each book and all the information from the book
  • HINT!!!!
    • lets say we have a “book” object we created and want to find the author
      • We would use  <name_of_book_variable>.author
      • Ex.  book_we_want_to_add.author  in our add_books function will give the author
  • So your general steps should be
    • 1. Get all the keys in your dictionary (or use items to get keys and values)
    • 2. in a for loop, you need to go through each key:value and get the book (it should be the value)
    • 3. once you get the book, use the hint to print the following message in the for loop
    • f” The book <book name> is written by <author> on <publish_year>  with the id <id>”

Part 3 and 4

I want everyone to attempt this

  • Create two more functions  def search_by_name(self) and search_by_id(self)

Your general steps is

  • using input, ask for the name or id you are looking for
  • use a for loop to go through the key’s of the right dictionary (either the name dicitionary or the id dictionary)
  • if their input == a name or id in that dictionary, print the same message as the Part 2

 

So here, i wanted to include the code we worked on if you need to catch up . If you are using my template, your goal is the replace the pass, with code and add one line with in add_book

# this is our main file(main.py
from book import Book

class LibrarySystem:
    def __init__(self):
        self.menu = """
        Please look at below options
        1. Add book
        2. List all books
        3. Find book by name
        4. Find book by id
        5. List all old books
        Enter your selection. Enter 'quit' to exit: """
        self.book_dictionary = {}  # looks things up with name as key
        self.books_by_id = {} # looks thing up with id as key

    def add_book(self):
        #title,author, publish_year,id
        title = input("What is the name of the book? : ")
        author= input("What is the author of the book? :")
        publish_year= input("what year was it published? :")
        id= input("what is the id of the book")

        book_we_want_to_add =Book(title,author,publish_year,id )

        #adding the book in the name dictionary
        self.book_dictionary[title]= book_we_want_to_add

        #adding the book in the id dictionary 
        
    def list_all_books(self):
        pass
    def search_by_name(self):
        pass
    def books_by_id(self):
        pass


Our book file
class Book:
    def __init__(self,title,author, publish_year,id):
        self.title=title
        self.author=author
        self.publish_year=publish_year
        self.id=id

Below, I have put some code to test if your code works (you need to add a book to test it). Put it in the bottom of your code to work

system = LibrarySystem()
#adds the books
system.add_book()
# make sure there is two print statements so that you add the dictionary to both
print(system.book_dictionary)
print(system.books_by_id)
# testing parts 2,3,4
system.list_all_books()
system.search_by_name() 
system.search_by_id()

Details

Date:
July 17
Time:
6:00 pm - 7:00 pm