【Flying Baidu Pilot Group Zero Basic Python】Study Notes

Flying paddle Baidu pilot group big operation

The Flying Oar Leader Group is an interest community for flying oar developers, providing developers with a rich local technology salon, Meetup, and online communication platform, open to all developers interested in the field of artificial intelligence and deep learning. With the enthusiastic support of the leaders and members of the pilot groups of various cities/universities, the Flying Oars Pilot Group has established 132 communities, covering 28 provincial administrative regions and 108 colleges and universities, and they are continuing to grow. Developers are welcome to join the pilot group, get to know more local technology partners, build an open source community, and share open source achievements and happiness.

Define the Student class, including the name, dob, age, gender, and score attributes, including the top3 method used to return the maximum 3 grades of the student (repeatable), and the sanitize method used to turn negative scores into positive scores, and negative ones The score may be entered incorrectly. Declare the number of stu_list object groups to store all student objects. Finally, output all student information including name, birthday, age, gender, and the highest 3 scores.
Insert picture description here

# 请在此处完成代码
def opentext(txt):
    with open(txt) as f:
        line = f.readline()
        # print(line.strip().split(','))
    return line.strip().split(',')

      

class Student():
    def __init__(self, name, dob, age, gender, score):
        self.name = name
        self.dob = dob
        self.age = age
        self.gender = gender
        self.score = score

    def name(self):
        return self.name

    def dob(self):
        return self.dob
    
    def age(self):
        return self.age

    def gender(self):
        return self.gender

    def top3(self):
        newlist = []
        for t in self.score:
            t = int(t)
            if t < 0 :
                t = -t
            newlist.append(t)
        return sorted(newlist)[::-1][:3]
            

def printf(textname):
    txtlist = opentext(textname)
    name = txtlist.pop(0)
    dob = txtlist.pop(0)
    age = txtlist.pop(0)
    gender = txtlist.pop(0)
    student = Student(name, dob, age, gender, txtlist)
    # print(student.name)a
    print(f'姓名:{student.name} 生日:{student.dob} 年龄:{student.age} 性别:{student.gender} 分数:{student.top3()}')


printf('work/stu1.txt')
printf('work/stu2.txt')
printf('work/stu3.txt')
printf('work/stu4.txt')

stu5.txt Specialty classmates, 2020-10-5, 20,'male', 180,87,98,77,76,92,58,-76,84,69, -47
stu6.txt Specialty classmates, 2020-10 -6,20,'female',230,76,48,82,88,92,58,-91,84,69,-68 The
above two txt files can be found under the work path.

Define Spostudent and Artstudent as subclasses of Student, and add spe as the specialty score in the properties of the subclass. The top3 method included in Spostudent returns the lowest 3 scores (repeatable), and the Artstudent including top3 method returns the highest 3 scores (repeatable). Finally, the names and birthdays of 2 special students are output using polymorphism. , Age, gender, score, specialty score.
Insert picture description here


# 请在此处完成代码
class Spostudent(Student):
    def __init__(self, name, dob, age, gender, score, spe):
        Student.__init__(self, name, dob, age, gender, score)
        self.spe = spe

    def spe(self):
        return self.spe
    
class Artstudent(Student):
    def __init__(self,  name, dob, age, gender, score):
        Student.__init__(self, name, dob, age, gender, score)
    
    def top3(self):
        newlist = []
        for t in self.score:
            t = int(t)
            if t < 0 :
                t = -t
            newlist.append(t)
        return sorted(newlist)[0:3]

def printf(text,a):
    txtlist = opentext(text)
    name = txtlist.pop(0)
    dob = txtlist.pop(0)
    age = txtlist.pop(0)
    gender = txtlist.pop(0)
    spe = txtlist.pop(0)
    spostudent = Spostudent(name, dob, age, gender, txtlist, spe)
    artstudent = Artstudent(name, dob, age, gender, txtlist)
    if a == 0:
        a = spostudent.top3()
    else:
        a = artstudent.top3()
    print(f'姓名:{spostudent.name} 生日:{spostudent.dob} 年龄:{spostudent.age} 性别:{spostudent.gender} 分数:{a} 特长分:{spostudent.spe}')

        
printf('work/stu5.txt', 1)


        
printf('work/stu5.txt', 1)
printf('work/stu6.txt', 0)

Lesson experience

The six days passed very quickly, and the meaning is still not enough. The experience is over before I have a good experience. I will continue to pay attention to the harvest.
The use of the platform is like a tool book, and it
will be fine after the basic use.

Guess you like

Origin blog.csdn.net/AzirBoDa/article/details/113772319