定义一个学生类,包含学号、姓名和考试成绩等基本信息。通过键盘输入学生信息,并将学生信息保存到文件studentscore.obj中。最后读取文件中的学生信息,并在控制台中显示出来。
时间: 2023-09-11 18:05:18 浏览: 178
一个 Student类:包括姓名、学号、性别、专业、家庭地址。
### 回答1:
好的,我会为您提供代码实现。
首先,我们定义一个Student类,包含学号、姓名和考试成绩三个属性。
```python
class Student:
def __init__(self, id, name, score):
self.id = id
self.name = name
self.score = score
```
然后,我们通过键盘输入学生信息,并将学生信息保存到文件studentscore.obj中。
```python
import pickle
students = []
while True:
id = input("请输入学号(输入exit结束输入):")
if id == "exit":
break
name = input("请输入姓名:")
score = input("请输入考试成绩:")
student = Student(id, name, score)
students.append(student)
with open("studentscore.obj", "wb") as f:
pickle.dump(students, f)
```
最后,我们从文件中读取学生信息,并在控制台中显示出来。
```python
with open("studentscore.obj", "rb") as f:
students = pickle.load(f)
for student in students:
print("学号:{},姓名:{},考试成绩:{}".format(student.id, student.name, student.score))
```
完整代码如下:
```python
import pickle
class Student:
def __init__(self, id, name, score):
self.id = id
self.name = name
self.score = score
students = []
while True:
id = input("请输入学号(输入exit结束输入):")
if id == "exit":
break
name = input("请输入姓名:")
score = input("请输入考试成绩:")
student = Student(id, name, score)
students.append(student)
with open("studentscore.obj", "wb") as f:
pickle.dump(students, f)
with open("studentscore.obj", "rb") as f:
students = pickle.load(f)
for student in students:
print("学号:{},姓名:{},考试成绩:{}".format(student.id, student.name, student.score))
```
希望这个代码对您有所帮助。
### 回答2:
定义一个学生类可以包含学号(id)、姓名(name)和考试成绩(score)等基本信息。
```python
class Student:
def __init__(self, id, name, score):
self.id = id
self.name = name
self.score = score
def __str__(self):
return f"学号:{self.id}\t姓名:{self.name}\t成绩:{self.score}"
# 从键盘输入学生信息
def input_student_info():
id = input("请输入学号:")
name = input("请输入姓名:")
score = input("请输入成绩:")
return Student(id, name, score)
# 将学生信息保存到文件
def save_student_info(students):
try:
with open("studentscore.obj", "w") as file:
for student in students:
file.write(f"{student.id},{student.name},{student.score}\n")
print("学生信息保存成功!")
except Exception as e:
print("保存学生信息失败!", e)
# 从文件中读取学生信息
def load_student_info():
students = []
try:
with open("studentscore.obj", "r") as file:
for line in file:
data = line.strip().split(",")
student = Student(data[0], data[1], data[2])
students.append(student)
except Exception as e:
print("读取学生信息失败!", e)
return students
# 主程序
def main():
students = []
num = int(input("请输入学生人数:"))
for i in range(num):
student = input_student_info()
students.append(student)
save_student_info(students)
loaded_students = load_student_info()
# 显示学生信息
for student in loaded_students:
print(student)
if __name__ == "__main__":
main()
```
通过上述代码可以实现对学生信息的录入和保存到文件`studentscore.obj`中,然后再从该文件中读取学生信息并在控制台中显示出来。
### 回答3:
定义一个学生类如下:
```python
class Student:
def __init__(self, student_id, name, score):
self.student_id = student_id
self.name = name
self.score = score
```
接下来,我们从键盘输入学生信息,并将学生信息保存到文件studentscore.obj中:
```python
import pickle
# 创建学生对象并输入信息
student_id = input('请输入学生学号:')
name = input('请输入学生姓名:')
score = input('请输入学生考试成绩:')
student = Student(student_id, name, score)
# 将学生对象写入文件
with open('studentscore.obj', 'wb') as file:
pickle.dump(student, file)
```
最后,我们读取文件中的学生信息,并在控制台中显示出来:
```python
import pickle
# 从文件中读取学生对象
with open('studentscore.obj', 'rb') as file:
student = pickle.load(file)
# 在控制台中显示学生信息
print('学生学号:', student.student_id)
print('学生姓名:', student.name)
print('学生考试成绩:', student.score)
```
运行以上代码,可以实现从键盘输入学生信息,并将学生信息保存到文件中,最后读取文件中的学生信息并在控制台中显示出来。
阅读全文