编写函数print,打印一个学生成绩的数组,该数组中有5个学生的数据记录,每个记录包括num name score[3],用主函数输入这些记录,用print函数输出这些记录
时间: 2023-06-14 15:07:25 浏览: 162
下面是一个例子,实现了上述要求:
```python
class Student:
def __init__(self, num, name, scores):
self.num = num
self.name = name
self.scores = scores
def print_scores(students):
for student in students:
print("num: {}, name: {}, scores: {}".format(student.num, student.name, student.scores))
if __name__ == '__main__':
students = []
for i in range(5):
num = input("Please input the student number: ")
name = input("Please input the student name: ")
scores = input("Please input the student scores, separated by comma: ").split(",")
scores = [int(score) for score in scores]
student = Student(num, name, scores)
students.append(student)
print_scores(students)
```
这里定义了一个`Student`类,表示一个学生的数据记录,包括学号、姓名和3门课程的成绩。`print_scores`函数接受一个学生列表作为参数,遍历列表并打印每个学生的信息。在主函数中,使用循环输入5个学生的数据记录,将它们存储到`students`列表中,最后调用`print_scores`函数输出这些记录。
阅读全文