To manage students data

mail@pastecode.io avatarunknown
python
23 days ago
3.1 kB
3
Indexable
Never
# This is our Student class
class Student:
  
    # This sets up our student data
    def __init__(self, name, rollno, m1, m2, age, phone_number):
        self.name = name
        self.rollno = rollno
        self.m1 = m1
        self.m2 = m2
        self.age = age
        self.phone_number = phone_number

    # This adds a new student to the list
    @classmethod
    def accept(cls, Name, Rollno, marks1, marks2, Age, PhoneNumber):
        new_student = Student(Name, Rollno, marks1, marks2, Age, PhoneNumber)
        ls.append(new_student)

    # This shows the student's info
    @staticmethod
    def display(student):
        print("Name : ", student.name)
        print("RollNo : ", student.rollno)
        print("Marks1 : ", student.m1)
        print("Marks2 : ", student.m2)
        print("Age : ", student.age)
        print("Phone Number : ", student.phone_number)
        print("\n")

    # This finds a student by roll number
    @staticmethod
    def search(roll_number):
        for i in range(len(ls)):
            if ls[i].rollno == roll_number:
                return i
        return -1

    # This removes a student by roll number
    @classmethod
    def delete(cls, roll_number):
        i = cls.search(roll_number)
        if i != -1:
            del ls[i]
        else:
            print("Student not found")

    # This changes the roll number of a student
    @classmethod
    def update(cls, old_roll_number, new_roll_number):
        i = cls.search(old_roll_number)
        if i != -1:
            ls[i].rollno = new_roll_number
        else:
            print("Student not found")

# This is our list of students
ls = []

# These are the options you can choose
print("\nWhat would you like to do?")
print("\n1. Add a student\n2. Show all students\n3. Find a student\n4. Remove a student\n5. Change a student's roll number\n6. Quit")

# This part lets you pick an option
while True:
    choice = int(input("Pick a number: "))
    if choice == 1:
        name = input("Enter Name: ")
        rollno = int(input("Enter Roll No: "))
        m1 = int(input("Enter First Marks: "))
        m2 = int(input("Enter Second Marks: "))
        age = int(input("Enter Age: "))
        phone_number = input("Enter Phone Number: ")
        Student.accept(name, rollno, m1, m2, age, phone_number)
    elif choice == 2:
        print("\nAll students:")
        for i in range(len(ls)):
            Student.display(ls[i])
    elif choice == 3:
        roll_number = int(input("Which roll number to find?: "))
        i = Student.search(roll_number)
        if i != -1:
            Student.display(ls[i])
        else:
            print("Can't find that student.")
    elif choice == 4:
        roll_number = int(input("Which roll number to remove?: "))
        Student.delete(roll_number)
    elif choice == 5:
        old_roll_number = int(input("Which roll number to change?: "))
        new_roll_number = int(input("What's the new roll number?: "))
        Student.update(old_roll_number, new_roll_number)
    else:
        print("Okay, goodbye!")
        break