Write a program to Student height record program for a school in Python


The Python code defines a class called SchoolHeightRecords that allows you to manage height records of students. Here's a breakdown of the code:

  • The SchoolHeightRecords class is defined with the following methods:
    • __init__: Initializes an empty dictionary called student_records to store student names and their corresponding heights.
    • add_student: Adds a new student's height to the student_records dictionary.
    • remove_student: Removes a student's record from the student_records dictionary by providing the student's name.
    • find_student_height: Retrieves the height of a student by providing their name. If the student is not found, it returns None.
    • display_records: Displays all the student height records stored in the dictionary.
  • An instance of the SchoolHeightRecords class is created with the variable school_records.
  • A menu is displayed to the user with the following options:
    • 1. Add Student Height
    • 2. Remove Student Height
    • 3. Find Student Height
    • 4. Display All Records
    • 5. Quit
  • The program enters a loop where the user is prompted to enter their choice.
  • Depending on the user's choice, the corresponding method of the SchoolHeightRecords instance is called to perform the desired operation. The user can add, remove, find, or display student height records, or quit the program.
  • If the user provides an invalid choice, they are notified with the "Invalid choice" message and prompted to try again.

The code allows you to interactively manage student height records, and it will continue running until the user chooses to quit (option 5).

One note is that the heights are stored as strings in the student_records dictionary. If you need to perform numerical operations with the heights, you should convert them to integers or floats before using them in calculations.


Source Code

class SchoolHeightRecords:
    def __init__(self):
        self.student_records = {}
 
    def add_student(self, student_name, height):
        self.student_records[student_name] = height
 
    def remove_student(self, student_name):
        if student_name in self.student_records:
            del self.student_records[student_name]
            print(f"{student_name} Records Remove Success")
        else:
            print(f"{student_name} Not Found in Records")
 
    def find_student_height(self, student_name):
        if student_name in self.student_records:
            return self.student_records[student_name]
        else:
            return None
 
    def display_records(self):
        print("Student Height Records:")
        for student, height in self.student_records.items():
            print(f"{student} : {height} cm")
 
 
school_records = SchoolHeightRecords()
 
print("1. Add Student Height")
print("2. Remove Student Height")
print("3. Find Student Height")
print("4. Display All Records")
print("5. Quit")
 
while True:
 
    choice = int(input("\nEnter your Choice : "))
 
    if choice == 1:
        student_name = input("Enter Student Name : ")
        height = input("Enter Student Height (in cm) : ")
        school_records.add_student(student_name.upper(), height)
        print(f"{student_name}'s Height Added to Records.")
    elif choice == 2:
        student_name = input("Enter Student Name to Remove : ")
        school_records.remove_student(student_name.upper())
    elif choice == 3:
        student_name = input("Enter Student Name to find Height : ")
        height = school_records.find_student_height(student_name.upper())
        if height is not None:
            print(f"{student_name}'s Height : {height} cm")
        else:
            print(f"{student_name} Not Found in Records")
    elif choice == 4:
        school_records.display_records()
    elif choice == 5:
        break
    else:
        print("Invalid choice. Please try again")
 

Output

1. Add Student Height
2. Remove Student Height
3. Find Student Height
4. Display All Records
5. Quit

Enter your Choice : 1
Enter Student Name : Siva
Enter Student Height (in cm) : 165.67
Siva's Height Added to Records.

Enter your Choice : 1
Enter Student Name : Sathish
Enter Student Height (in cm) : 196.78
Sathish's Height Added to Records.

Enter your Choice : 1
Enter Student Name : Diya
Enter Student Height (in cm) : 145.45
Diya's Height Added to Records.

Enter your Choice : 1
Enter Student Name : Pooja
Enter Student Height (in cm) : 167.32
Pooja's Height Added to Records.

Enter your Choice : 4
Student Height Records:
SIVA : 165.67 cm
SATHISH : 196.78 cm
DIYA : 145.45 cm
POOJA : 167.32 cm

Enter your Choice : 3
Enter Student Name to find Height : diya
diya's Height : 145.45 cm

Enter your Choice : 2
Enter Student Name to Remove : pooja
POOJA Records Remove Success

Enter your Choice : 4
Student Height Records:
SIVA : 165.67 cm
SATHISH : 196.78 cm
DIYA : 145.45 cm

Enter your Choice : 5

Example Programs