Python 7 Levels, L5, Unit 47, Object Oriented Exercises

Powered By EmbedPress

Exercise:

class Hotdog():

    def __init__(self,cooked_level,cooked_string,condiments):
        self.cooked_level = cooked_level
        self.cooked_string = cooked_string
        self.condiments = condiments

    def cook(self,time):
        print("Cooking hot dog for ",time," minutes.")
        self.cooked_level = self.cooked_level + time
        if self.cooked_level > 8:
            self.cooked_string = "Well done."
        elif self.cooked_level > 5:
            self.cooked_string = "Done."
        elif self.cooked_level > 3:
            self.cooked_string = "Medium"
        else:
            self.cooked_string = "Raw"

    def add_condiments(self,condiment):
        self.condiments.append(condiment)

    def __str__(self):
        msg = "Hot dog is:"
        if len(self.condiments) > 0:
            s = ""
            for i in self.condiments:
                s = s + i
            msg = msg + self.cooked_string + ", condiments are:"+s
        else:
            msg = msg + self.cooked_string
        return msg

my_Hotdog = Hotdog(0,"Raw",[])
print(my_Hotdog)
my_Hotdog.cook(7)
my_Hotdog.add_condiments("Spice sauce")
my_Hotdog.add_condiments("Ketchup")
print(my_Hotdog)

Homework:

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("Student added 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("Student "+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 succeeds.")

stu_list = []#create a new student list
stuAdmin = StuAdmin()
while True:
    num = input("Please pick an operation(1.Add Student 2.View All Student 3.Search 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 you delete:")
        stuAdmin.delet_stu(stu_list, name)
    elif num == "99":
        print("Abort system.")
        break

dahan1999

Leave a Reply

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

Related Posts