Python定义一个student类,包括学号,姓名,性别,语文,数学,英语,计算机等属性和计算总分和平均分的两个方法。实例化对象,输出学生姓名和总分及平均分
时间: 2024-06-08 11:12:23 浏览: 10
以下是Python定义一个student类的代码:
```python
class Student:
def __init__(self, number, name, sex, chinese, math, english, computer):
self.number = number
self.name = name
self.sex = sex
self.chinese = chinese
self.math = math
self.english = english
self.computer = computer
def total_score(self):
return self.chinese + self.math + self.english + self.computer
def avg_score(self):
return self.total_score() / 4
# 实例化对象
s = Student('001', '张三', '男', 90, 85, 95, 88)
# 输出学生姓名和总分及平均分
print(f'学生姓名:{s.name}')
print(f'总分:{s.total_score()}')
print(f'平均分:{s.avg_score()}')
```
输出结果如下:
```
学生姓名:张三
总分:358
平均分:89.5
```
相关问题
Jupyter Notebook定义一个student类,包括学号,姓名,性别,语文,数学,英语,计算机等属性和计算总分和平均分的两个方法。实例化对象,输出学生姓名和总分及平均分
下面是一个示例代码:
```python
class Student:
def __init__(self, id, name, gender, chinese, math, english, computer):
self.id = id
self.name = name
self.gender = gender
self.chinese = chinese
self.math = math
self.english = english
self.computer = computer
def total_score(self):
return self.chinese + self.math + self.english + self.computer
def avg_score(self):
return self.total_score() / 4
student = Student('001', '张三', '男', 80, 90, 85, 95)
print('学生姓名:{},总分:{},平均分:{}'.format(student.name, student.total_score(), student.avg_score()))
```
输出结果:
```
学生姓名:张三,总分:350,平均分:87.5
```
python学生管理系统求每门课程平均分和总分,每个学生的总分和平均分,按学号大小输出成绩表
好的,这是一个比较典型的学生管理系统需求,可以使用 Python 进行实现。首先,我们需要定义一些数据结构,比如学生类(包含学号、姓名、各科成绩等信息)、课程类(包含课程名称、学分等信息)等等。然后,我们需要读取学生信息和成绩信息,计算每门课程的平均分和总分,每个学生的总分和平均分,并按照学号大小排序输出成绩表。下面是一份简单的代码示例,仅供参考:
```python
# 定义学生类
class Student:
def __init__(self, id, name):
self.id = id
self.name = name
self.scores = {} # 存放各科成绩的字典
def add_score(self, course, score):
self.scores[course] = score
def total_score(self):
return sum(self.scores.values())
def avg_score(self):
return self.total_score() / len(self.scores)
# 定义课程类
class Course:
def __init__(self, name, credits):
self.name = name
self.credits = credits
# 读取学生信息和成绩信息
students = []
courses = []
with open('students.txt', 'r') as f:
for line in f:
id, name = line.strip().split()
students.append(Student(id, name))
with open('courses.txt', 'r') as f:
for line in f:
name, credits = line.strip().split()
courses.append(Course(name, int(credits)))
for student in students:
with open(f'{student.id}.txt', 'r') as f:
for line in f:
course, score = line.strip().split()
student.add_score(course, int(score))
# 计算每门课程的平均分和总分
course_scores = {course.name: {'total': 0, 'count': 0} for course in courses}
for student in students:
for course, score in student.scores.items():
course_scores[course]['total'] += score
course_scores[course]['count'] += 1
for course in courses:
avg_score = course_scores[course.name]['total'] / course_scores[course.name]['count']
print(f'{course.name} 平均分:{avg_score},总分:{course_scores[course.name]["total"]}')
# 计算每个学生的总分和平均分,并按照学号大小排序输出成绩表
students.sort(key=lambda x: x.id)
for student in students:
total_score = student.total_score()
avg_score = student.avg_score()
print(f'{student.id} {student.name} 总分:{total_score},平均分:{avg_score}')
```
其中,`students.txt` 文件包含学生信息,每行格式为 `<学号> <姓名>`;`courses.txt` 文件包含课程信息,每行格式为 `<课程名称> <学分>`;每个学生的成绩信息存放在以学号命名的文件中,每行格式为 `<课程名称> <成绩>`。在代码中,我们首先读取学生信息和成绩信息,然后计算每门课程的平均分和总分,最后计算每个学生的总分和平均分,并按照学号大小排序输出成绩表。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.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)