0% found this document useful (0 votes)
43 views5 pages

PYTHON Record MSC Computer Science

The document describes two programs: a Library Management System and a Student Grading System. The Library Management System allows users to add, search, borrow, and return books, with the borrow_book method checking if the book exists and if copies are available before allowing a borrow. The Student Grading System calculates grades based on average marks, with the calculate_grade method assigning letter grades from A to F based on the average score.

Uploaded by

Rohit Sahu
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
43 views5 pages

PYTHON Record MSC Computer Science

The document describes two programs: a Library Management System and a Student Grading System. The Library Management System allows users to add, search, borrow, and return books, with the borrow_book method checking if the book exists and if copies are available before allowing a borrow. The Student Grading System calculates grades based on average marks, with the calculate_grade method assigning letter grades from A to F based on the average score.

Uploaded by

Rohit Sahu
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

1.

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.

ANS :- Library Management System

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 add_book(self, title, author, copies):


self.books[title] = {"author": author, "copies": copies}
print(f"Book '{title}' added successfully!")

def search_book(self, title):


if title in self.books:
book = self.books[title]
print(f"Title: {title}, Author: {book['author']}, Copies Available: {book['copies']}")
else:
print("Book not found!")

def borrow_book(self, title):


if title in self.books and self.books[title]["copies"] > 0:
self.books[title]["copies"] -= 1
print(f"You have borrowed '{title}'.")
elif title in self.books:
print(f"Sorry, '{title}' is currently unavailable.")
else:
print("Book not found!")

def return_book(self, title):


if title in self.books:
self.books[title]["copies"] += 1
print(f"'{title}' has been returned. Thank you!")
else:
print("Book not found in library records!")

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()

Outputs of the Program

Library Management System

Input:

Library Management System


1. Add Book
2. Search Book
3. Borrow Book
4. Return Book
5. Exit
Enter your choice: 1
Enter book title: Python Programming
Enter book author: John Doe
Enter number of copies: 3

Enter your choice: 2


Enter book title to search: Python Programming

Enter your choice: 3


Enter book title to borrow: Python Programming

Enter your choice: 4


Enter book title to return: Python Programming

Enter your choice: 5

Output:

Book 'Python Programming' added successfully!

Title: Python Programming, Author: John Doe, Copies Available: 3

You have borrowed 'Python Programming'.

'Python Programming' has been returned. Thank you!

Exiting Library Management System. Goodbye!

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.

ANS : - Student Grading System

A program to calculate grades for students based on their 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

Student Grading System

Input:

Student Grading System


1. Add Student
2. Display All Students
3. Exit
Enter your choice: 1
Enter student's name: Alice
Enter marks separated by space: 85 90 88

Enter your choice: 1


Enter student's name: Bob
Enter marks separated by space: 70 75 80

Enter your choice: 2

Enter your choice: 3

Output:

Student 'Alice' added successfully!


Student 'Bob' added successfully!

Student Details:
Name: Alice
Marks: [85, 90, 88]
Grade: A

Name: Bob
Marks: [70, 75, 80]
Grade: C

Exiting Student Grading System. Goodbye!

You might also like