Intro. to Python Programming (CS 153)
I enrolled into the Intro Programming with Python (CS 153) course in Spring 2024. This course taught me how to program in Python and learned the skill of repetition and how it helped me refine my skills in coding. One coding assignment that stood out among the others was that we were prompted to create a “library management” that would tell the user what was checked out, what was added to the library, and what was in stock. This assignment was challenging in many ways, but with help from the professor and assistants, I was able to fully understand and complete the task at hand. In the professional world, I would consider this as a skill of persistence, in which I will keep working and try to get help if necessary.
Below is the python file (written out due to WordPress formatting) of the program “Library Management”:
# -*- coding: utf-8 -*- """ Name of source file: Exercise_11-Task2-ABRAHAM_PEREZ.py Student name: Abraham Perez Date: Fri Mar 29 2024 This program is used to upgrade a library management system to utilize a nested dictionary instead of the list. """ books= { "Harry Potter": {"copies": 5, "loaned": 0, "loaned_to":[]}, "Lord of the rings": {"copies": 3, "loaned": 0, "loaned_to":[]}, "Pride and Prejudice": {"copies": 4, "loaned": 0, "loaned_to":[]}, #additional books here } loaned_books_count= 0 #add_book function uses if statement for when a title is not in dictionary, #and uses else and increases copies number def add_book(title, copies): if title not in books: books[title]= {'copies': copies, 'loaned': 0, 'loaned_to': []} else: books[title]["copies"] += copies print(f"Added '{copies}' copies of {title} to the library.") #loan_out function checks if book can be loaned out to someone, and enters name to loaned_to list, #and if not print stating it's not available for loan def loan_out(title, borrower_name): if title in books: books[title]["loaned"] += 1 books[title]["loaned_to"].append(borrower_name) print(f"Loaned out '{title}' to '{borrower_name}'.") return True else: print(f'Book {title} is not available for loan.') return False #return_book function checks if book can be returned, #if not prints that it can't and it wasn't loaned to borrower def return_book(title, borrower_name): if title in books and borrower_name in books[title]["loaned_to"]: books[title]["loaned"] -= 1 books[title]["loaned_to"].remove(borrower_name) print(f"'{borrower_name}' has returned '{title}'.") else: print(f"Can't return '{title}'. It was not loaned out to '{borrower_name}'.") #display_inventory function displays all titles, number of copies, and how many were loaned def display_inventory(): for title, info in books.items(): print(f'{title}, Copies: {info["copies"]} Loaned: {info["loaned"]}') #display_loan_history function prints if any books have been loaned out to, #if not, prints that all copies are available def display_loan_history(): for title, info in books.items(): borrowers= ', '.join(info['loaned_to']) if borrowers: print(f"'{title}' has been loaned out to: {borrowers}") else: print(f"All '{title}' copies are available.") #search_for_book function checks if book is alrady in dictionary, #if not, prints that it can't be found def search_for_book(query): found= False for title in books.keys(): if query.lower() in title.lower(): available= books[title]["copies"] - books[title]["loaned"] borrowers= ', '.join(books[title]['loaned_to']) print(f"'{title}' - Available Copies: {available}, Borrowers: {borrowers}") found= True if not found: print(f"No books matching '{query}' is found in the library inventory.") #Sample run add_book("The Hobbit", 2) print(50*"*") loan_out("Harry Potter", "Alice") print(50*"*") loan_out("Pride and Prejudice", "Bob") print(50*"*") return_book("Harry Potter", "Alice") print(50*"*") display_inventory() print(50*"*") display_loan_history() print(50*"*") search_for_book("Pride") print(50*"*") search_for_book("The Great Gatsby") print(50*"*")