Library Menu in Python
Creating a library menu in Python involves designing a simple interface that allows users to interact with a list of options, such as borrowing or returning books. It’s like building a small catalog system, where users can select from a few options and see different results based on their choices.
This project might seem simple, but it covers a lot of important programming concepts, like functions, loops, conditionals, and user input handling.
Table of Contents
Components of Library Menu
To create a simple library menu in Python, you’ll need to focus on a few essential components:
- A list of available books: You need a list or a similar structure to represent the books in the library.
- Menu options: A clear and simple menu, offering choices such as borrowing a book, returning a book, or viewing all books.
- User input handling: Efficient handling of what the user selects to execute the right action.
- Functions: Each menu option should be tied to a function that performs a specific task.
Online File Sharing System Using PHP and MySQL: A Step-by-Step Guide
Build Menu in Python
Menu Display
def display_menu():
print("Welcome to the Library!")
print("1. View all books")
print("2. Borrow a book")
print("3. Return a book")
print("4. Exit")
Handling User Input
User input is collected using Python’s built-in input()
function. This allows you to grab the user’s choice and respond accordingly.
user_choice = input("Please select an option: ")
Source Code
Below is the complete source code for a simple Python-based library menu system:
# List of books in the library
books = ["The Great Gatsby", "1984", "To Kill a Mockingbird", "Pride and Prejudice"]
# Function to display the menu
def display_menu():
print("\nLibrary Menu:")
print("1. View all books")
print("2. Borrow a book")
print("3. Return a book")
print("4. Exit")
# Function to view books
def view_books():
print("\nAvailable books:")
for book in books:
print(f"- {book}")
# Function to borrow a book
def borrow_book():
book = input("Enter the name of the book you want to borrow: ")
if book in books:
books.remove(book)
print(f"You have borrowed '{book}'.")
else:
print("Sorry, that book is not available.")
# Function to return a book
def return_book():
book = input("Enter the name of the book you want to return: ")
books.append(book)
print(f"You have returned '{book}'.")
# Main loop
while True:
display_menu()
choice = int(input("\nSelect an option: "))
if choice == 1:
view_books()
elif choice == 2:
borrow_book()
elif choice == 3:
return_book()
elif choice == 4:
print("Thank you for using the library system. Goodbye!")
break
else:
print("Invalid choice. Please select a valid option.")
Breakdown the Code
1. The Book List
At the start, we have a simple list that stores book names:
books = ["The Great Gatsby", "1984", "To Kill a Mockingbird", "Pride and Prejudice"]
You can modify this list to add or remove books based on what your library offers.
2. The Menu Display
The display_menu
function prints out the available options for the user.
3. Viewing Books
The view_books
function iterates through the books
list and prints out each available book.
4. Borrowing and Returning Books
The borrow_book
function checks if a book is available and, if it is, removes it from the list. On the other hand, return_book
adds the book back to the list.
5. The Main Loop
The while
loop keeps the menu running until the user selects “Exit”.
Creating a simple library menu in Python is an excellent beginner project. It’s practical, easy to understand, and helps reinforce fundamental programming concepts. Whether you’re just starting your coding journey or looking to add a fun project to your portfolio, this library menu is a great stepping stone.
New Project :-https://www.youtube.com/@Decodeit2