PYTHON Record MSC Computer Science
PYTHON Record MSC Computer Science
For the Library Management System program, explain how the borrow_book method works
and describe the conditions it checks before allowing a user to borrow a book.
A program to manage books in a library, allowing users to add, search, borrow, and return
books.
class Library:
def __init__(self):
self.books = {}
def main():
lib = Library()
while True:
print("\nLibrary Management System")
print("1. Add Book")
print("2. Search Book")
print("3. Borrow Book")
print("4. Return Book")
print("5. Exit")
choice = int(input("Enter your choice: "))
if choice == 1:
title = input("Enter book title: ")
author = input("Enter book author: ")
copies = int(input("Enter number of copies: "))
lib.add_book(title, author, copies)
elif choice == 2:
title = input("Enter book title to search: ")
lib.search_book(title)
elif choice == 3:
title = input("Enter book title to borrow: ")
lib.borrow_book(title)
elif choice == 4:
title = input("Enter book title to return: ")
lib.return_book(title)
elif choice == 5:
print("Exiting Library Management System. Goodbye!")
break
else:
print("Invalid choice. Please try again.")
if __name__ == "__main__":
main()
Input:
Output:
2. In the Student Grading System program, write and explain the logic used in the
calculate_grade method for assigning grades based on average marks.
class Student:
def __init__(self, name, marks):
self.name = name
self.marks = marks
def calculate_grade(self):
avg = sum(self.marks) / len(self.marks)
if avg >= 90:
return 'A'
elif avg >= 80:
return 'B'
elif avg >= 70:
return 'C'
elif avg >= 60:
return 'D'
else:
return 'F'
def display_details(self):
print(f"Name: {self.name}")
print(f"Marks: {self.marks}")
print(f"Grade: {self.calculate_grade()}")
def main():
students = []
while True:
print("\nStudent Grading System")
print("1. Add Student")
print("2. Display All Students")
print("3. Exit")
choice = int(input("Enter your choice: "))
if choice == 1:
name = input("Enter student's name: ")
marks = list(map(int, input("Enter marks separated by space: ").split()))
student = Student(name, marks)
students.append(student)
print(f"Student '{name}' added successfully!")
elif choice == 2:
if not students:
print("No students added yet!")
else:
print("\nStudent Details:")
for student in students:
student.display_details()
print()
elif choice == 3:
print("Exiting Student Grading System. Goodbye!")
break
else:
print("Invalid choice. Please try again.")
if __name__ == "__main__":
main()
Outputs of the Program
Input:
Output:
Student Details:
Name: Alice
Marks: [85, 90, 88]
Grade: A
Name: Bob
Marks: [70, 75, 80]
Grade: C