python通过for循环,配合input键盘输入,并构造方法,完成学生信息的录入
时间: 2023-06-08 18:05:42 浏览: 305
好的,我明白了。对于你的问题,可以通过以下代码实现:
```python
class Student:
def __init__(self, name, age, scores):
self.name = name
self.age = age
self.scores = scores
def display(self):
print(f"学生姓名:{self.name}")
print(f"学生年龄:{self.age}")
print("=" * 20)
for i, s in enumerate(self.scores):
print(f"科目{i+1}成绩:{s}")
students = []
while True:
name = input("请输入学生姓名:")
if name == "":
break
age = int(input("请输入学生年龄:"))
scores = []
for i in range(3):
score = int(input(f"请输入第{i+1}门成绩:"))
scores.append(score)
student = Student(name, age, scores)
students.append(student)
print("=" * 20)
for i, student in enumerate(students):
print(f"第{i+1}名学生信息:")
student.display()
```
以上代码实现了一个学生信息的录入系统,通过输入姓名、年龄和三门成绩,将学生信息存入列表中,并通过display()函数展示学生信息。在录入时,通过while循环判断是否继续录入,通过for循环输入三门成绩,最后将所有学生的信息依次展示。
阅读全文
相关推荐
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)