Python 7 Levels, L5, Unit 48, Student Information Management System

Powered By EmbedPress

Exercise:

class Student():

    def __init__(self,ID,name,age):
        self.ID = ID
        self.name = name
        self.age = age

    def __str__(self):
        msg = self.ID+"  \t"+self.name+"\t"+str(self.age)
        return msg

class StuAdmin():

    def add_stu(self,stu_list):
        ID = input("Please input student's ID:")
        name =input("Please input student's name:")
        age = int(input("Please input student's age:"))
        stu = Student(ID,name,age)
        stu_list.append(stu)
        print("Add successfully.")
        print("ID\tName\tAge")
        print(stu)

    def show_stu(self,stu_list):
        print("ID\tName\tAge")
        for stu in stu_list:
            print(stu)

    def seek_stu(self,stu_list,name):
        for i in range(len(stu_list)):
            if stu_list[i].name == name:
                print("ID\tName\tAge")
                print(stu_list[i])
                return i
        else:
            print(name+" does not exist.")
            return -1

    def delet_stu(self,stu_list,name):
        index = self.seek_stu(stu_list,name)
        if index != -1:
            del stu_list[index]
            print("Delete successfully.")

stu_list = []#create a new empty student list
stuAdmin = StuAdmin()
while True:
    num = input("Please pick an operation(1.Add Student 2.View All Student 3.Look for Student 4.Delete Student 99.Quit)")
    if num == "1":
        stuAdmin.add_stu(stu_list)
    elif num == "2":
        stuAdmin.show_stu(stu_list)
    elif num == "3":
        name = input("Please input student name you look for:")
        stuAdmin.seek_stu(stu_list,name)
    elif num == "4":
        name = input("Please input student name to be deleted:")
        stuAdmin.delet_stu(stu_list, name)
    elif num == "99":
        print("Abort system")
        break

Homework:

class Student():

    def __init__(self,ID,name,age,science = 0,math = 0,english = 0,sum_score = 0):#subject score defaults to 0
        self.ID = ID
        self.name = name
        self.age = age
        self.science = science
        self.english = english
        self.math = math
        self.sum_score = self.science + self.english + self.math

    def __str__(self):
        msg = self.ID+"  \t"+self.name+"\t"+str(self.age)+"\t"+str(self.science)\
        +"\t"+str(self.math)+"\t"+str(self.english)+"\t"+str(self.sum_score)
        return msg

class StuAdmin():

    def add_stu(self,stu_list):
        ID = input("Please input student's ID:")
        name =input("Please input student's name:")
        age = int(input("Please input student's age:"))
        stu = Student(ID,name,age)
        stu_list.append(stu)
        print("Add student successfully.")
        print("ID.\tName\tAge\tScience\tMath\tEnglish\tScore")
        print(stu)

    def show_stu(self,stu_list):
        print("ID.\tName\tAge\tScience\tMath\tEnglish\tScore")
        for stu in stu_list:
            print(stu)

    def seek_stu(self,stu_list,name):
        for i in range(len(stu_list)):
            if stu_list[i].name == name:
                print("ID.\tName\tAge\tScience\tMath\tEnglish\tScore")
                print(stu_list[i])
                return i
        else:
            print(name+" does not exist.")
            return -1

    def delet_stu(self,stu_list,name):
        index = self.seek_stu(stu_list,name)
        if index != -1:
            del stu_list[index]
            print("Delete successfully.")

    def in_score(self,stu_list,name):
        index = self.seek_stu(stu_list,name)
        if index != -1:
            science = int(input("Please input "+stu_list[index].name+"'s science score:"))
            math = int(input("Please input "+stu_list[index].name+"'s math score:"))
            english = int(input("Please input "+stu_list[index].name+"'s English Score"))
            stu_list[index].science = science
            stu_list[index].math = math
            stu_list[index].english = english
            stu_list[index].sum_score = stu_list[index].science\
            + stu_list[index].math + stu_list[index].english
            print("Score add successfully.")

    def sum_sort(self,stu_list):
        sum_temp = []
        for stu in stu_list:#get total score from student list,store to sum_temp
            sum_temp.append(stu.sum_score)
        stu_list1 = stu_list[:]#duplicate a list for later sorting
        for i in range(len(stu_list1)-1):#bubble sort
            for j in range(len(stu_list1)-1-i):
                if sum_temp[j] < sum_temp[j+1]:
                    ss = sum_temp[j]
                    sum_temp[j] = sum_temp[j+1]
                    sum_temp[j+1] = ss

                    stuu = stu_list1[j]
                    stu_list1[j] = stu_list1[j+1]
                    stu_list1[j+1] = stuu

        print("ID.\tName\tAge\tScience\tMath\tEnglish\tScore")
        for stu in stu_list1:#output after bubble sort
            print(stu)
        

stu_list = []#create empty student list
stuAdmin = StuAdmin()
while True:
    num = input("Please pick an operation(1.Add Student 2.View All Students.\
3.Look for Student 4.Delete Student 5.Add scores 6.Sort Based on Total Score 99.Quit)")
    if num == "1":
        stuAdmin.add_stu(stu_list)
    elif num == "2":
        stuAdmin.show_stu(stu_list)
    elif num == "3":
        name = input("Please input student's name to be look for:")
        stuAdmin.seek_stu(stu_list,name)
    elif num == "4":
        name = input("Please input student's name to be deleted:")
        stuAdmin.delet_stu(stu_list, name)
    elif num == "5":
        name = input("Please input student's name to be added:")
        stuAdmin.in_score(stu_list, name)
    elif num == "6":
        stuAdmin.sum_sort(stu_list)
    elif num == "99":
        print("Quit.")
        break

dahan1999

1 Comment

Leave a Reply

Your email address will not be published. Required fields are marked *

Related Posts