7 Comments

Hi Ardit - Thanks for the Project. I am currently following your tutorials on oops in Udemy.

Please find the code below let me know how to optimize/improvise the code further. I would like to become a hands on OOPS developer in python. Your feedback helps me alot.

import sys

class BookLendingSystem:

def __init__(self):

self.available_books = {

1: "The Great Gatsby",

2: "To Kill a Mockingbird",

3: "1984",

4: "Pride and Prejudice"

}

self.borrowed_books = {}

def display_menu(self):

print("\nWelcome to the Book Lending System!")

menu = {

1 : "View Available Books" ,

2 : "Borrow a Book",

3 : "Return a Book",

4 : "View Borrowed Books",

5 : "Exit"

}

for key, value in menu.items():

print(f"{key}. {value}")

def view_available_books(self):

print("--- The Available Books ---")

for key, value in self.available_books.items():

print(f"{key}. {value}")

def borrow_book(self):

self.view_available_books()

book_num = int(input("\nEnter the book number to borrow: ").strip())

self.name = input("\nEnter your name: ").strip()

if not self.available_books:

pass

else:

for key, value in self.available_books.items():

if key == book_num:

print(f'You have borrowed "{value}". Please return it on time.')

self.borrowed_books[key] = value

def view_borrowed_books(self):

if not self.borrowed_books:

pass

else:

print("\n--- Borrowed Books ---")

for key, value in self.borrowed_books.items():

print(f"{key}. {value} - Borrowed by {self.name}.")

def return_book(self):

if not self.borrowed_books:

pass

else:

self.view_borrowed_books()

book_num = int(input("\nEnter the book number to return: ").strip())

for key, value in self.borrowed_books.items():

print(f'Thank you, {self.name}, for returning "{value}".')

def run(self):

while True:

self.display_menu()

choice = input("\nChoose an option: ").strip()

if choice == "1":

self.view_available_books()

if choice == "2":

self.borrow_book()

if choice == "3":

self.return_book()

if choice == "4":

self.view_borrowed_books()

if choice == "5":

sys.exit()

system = BookLendingSystem()

system.run()

Expand full comment

Hi, I would happily provide feedback, but please paste the code on a site such as pastebin.com and share the link here. The code as it appears in the Substack comments is very unreadable. I hope Substack changes this soon.

Expand full comment

Hi Ardit - Thank you very much for the quick response.

Here is the link: https://pastebin.com/rxVVx855

Please suggest me how to improvise/optimize the code further.

Looking forward to your feedback.

Thanks,

Dev

Expand full comment

Your code looks good here!

Expand full comment

Thank you very much, Ardit.

I would like to code more python oops projects. Please let me know if there are any python projects to focus on.

Thanks,

Dev

Expand full comment

The order doesn't matter. Just pick the correct level for you (i.e., beginner, intermediate, or real-world). I think you're not a beginner from what I see, so focus on the list of intermediate projects and real-world projects.

Expand full comment

sure.Thank you.

Expand full comment