
刚刚学python,对于细节不了解,没什么思路,希望可以收获解答。用python写,希望可以给点思路
import random
class Student:
def __init__(self, id, name, score):
self.id = id
self.name = name
self.score = score
# 插入两个学生
def Insert_student(Student_table, student):
Student_table.append(student)
# Student_table.append(Student(12345, 'haha', 88))
# Student_table.append(Student(12346, 'nini', 90))
# 删除一个学生
def delet_student_byIndex(Student_table, index):
del Student_table[index]
# 根据学号查找学生
def find_studen_byId(Student_table, id):
for i in Student_table:
if i.id == id:
print("学号:{},姓名:{},分数:{}".format(i.id, i.name, i.score))
# 输出学生人数
def numOfStudent(Student_table):
return len(Student_table)
def PrintTop3(Student_table):
temp_list = sorted(Student_table, key=lambda x: x.score, reverse=True)
for i in temp_list[:3]:
print("学号:{},姓名:{},分数:{}".format(i.id, i.name, i.score))
def OddStudent(Student_table):
for i in Student_table:
if i.id % 2:
print("学号:{},姓名:{},分数:{}".format(i.id, i.name, i.score))
if __name__ == '__main__':
# 顺序表
Student_table = [Student(random.randint(10000, 99999), ''.join([chr(random.randint(97, 122)) for i in range(3)]),
random.randint(0, 100)) for j in range(12)]
Insert_student(Student_table, Student(12345, 'haha', 88))
Insert_student(Student_table, Student(12346, 'nini', 90))
print()
delet_student_byIndex(Student_table, 2)
print()
find_studen_byId(Student_table, 12345)
print()
PrintTop3(Student_table)
print()
OddStudent(Student_table)