Python Functions Question :-
Case:
A library system needs a book management application.
Problem:
Create a function to add a new book with its ISBN.
Write a function to search for a book by its ISBN.
Implement a function to display all books sorted by title.
# Function to add a new book
def add_book(isbn, title):
pass
# Function to search for a book by ISBN
def search_book(isbn):
pass
# Function to display all books sorted by title
def display_books():
pass
# Test the functions
add_book("101", "Clean Code")
add_book("102", "Effective Python")
add_book("103", "Designing Data-Intensive Applications")
search_book("101")
search_book("104")
display_books()
###################################################
Question:-
Organizing Python Codes Using Functions
Case:
A car rental company needs a system for managing rentals.
Problem:
Create functions for renting a car, returning a car, and displaying available cars.
Use a dictionary to store car availability and rented cars.
Organize the functions into a well-structured program.
# Car Rental System
# Initialize the car inventory
car_inventory = {
"SUV": 5,
"Sedan": 3,
"Hatchback": 4
}
# Dictionary to track rented cars
rented_cars = {}
# Function to rent a car
def rent_car(car_type, customer_name):
pass
# Function to return a car
def return_car(customer_name):
pass
# Function to display available cars
def display_available_cars():
pass
# Main Program
def main():
while True:
print("\nCar Rental System Menu")
print("1. Rent a Car")
print("2. Return a Car")
print("3. Display Available Cars")
print("4. Exit")
choice = input("Enter your choice (1-4): ")
if choice == "1":
car_type = input("Enter the car type (SUV/Sedan/Hatchback):
").capitalize()
customer_name = input("Enter your name: ")
rent_car(car_type, customer_name)
elif choice == "2":
customer_name = input("Enter your name: ")
return_car(customer_name)
elif choice == "3":
display_available_cars()
elif choice == "4":
print("Thank you for using the Car Rental System.")
break
else:
print("Invalid choice! Please try again.")
main()