Python定义一个student类,包括学号,姓名,性别,语文,数学,英语,计算机等属性和计算总分和平均分的两个方法。实例化对象,输出学生姓名和总分及平均分
时间: 2024-06-08 10:12:23 浏览: 131
以下是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
```
某班期末考试科目为数学、英语和计算机,有最多不超过30人参加考试,考试后要求: 1)计算每个学生的总分和平均分; 2)按总分成绩由高到低排出成绩的名次; 3)打印出名次表,表格内包括学生学号、各科分数、总分和平均分; 4)任意输入一个学号,能够查找出该学生在班级中的排名及其考试分数。
好的,下面是一个Python实现的示例代码:
```python
class Student:
def __init__(self, id, name, math_score, english_score, computer_score):
self.id = id
self.name = name
self.math_score = math_score
self.english_score = english_score
self.computer_score = computer_score
self.total_score = self.math_score + self.english_score + self.computer_score
self.average_score = self.total_score / 3
students = []
num_students = int(input("请输入班级人数:"))
for i in range(num_students):
id = input("请输入第%d个学生的学号:" % (i+1))
name = input("请输入第%d个学生的姓名:" % (i+1))
math_score = float(input("请输入第%d个学生的数学成绩:" % (i+1)))
english_score = float(input("请输入第%d个学生的英语成绩:" % (i+1)))
computer_score = float(input("请输入第%d个学生的计算机成绩:" % (i+1)))
student = Student(id, name, math_score, english_score, computer_score)
students.append(student)
# 按总分排序
students = sorted(students, key=lambda student: student.total_score, reverse=True)
# 打印名次表
print("名次\t学号\t姓名\t数学成绩\t英语成绩\t计算机成绩\t总分\t平均分")
for i, student in enumerate(students):
print("%d\t%s\t%s\t%.2f\t\t%.2f\t\t%.2f\t\t%.2f\t%.2f" % (i+1, student.id, student.name, student.math_score, student.english_score, student.computer_score, student.total_score, student.average_score))
# 查找学生排名及成绩
target_id = input("请输入要查找的学生学号:")
for i, student in enumerate(students):
if student.id == target_id:
print("学号为%s的学生排名为第%d名,其成绩为:数学%.2f,英语%.2f,计算机%.2f,总分%.2f,平均分%.2f" % (student.id, i+1, student.math_score, student.english_score, student.computer_score, student.total_score, student.average_score))
break
else:
print("未找到学号为%s的学生" % target_id)
```
你可以在命令行或者Python IDE中运行上述代码,根据提示输入班级人数、每个学生的学号、姓名、数学、英语和计算机成绩,程序会自动计算每个学生的总分和平均分,并按总分成绩由高到低排出成绩的名次,最后打印出名次表。你也可以输入一个学号来查找学生在班级中的排名及其考试分数。
阅读全文