Python输入学生n门课的成绩并计算平均分,总分,最高分,最低分分别输出
时间: 2023-12-08 22:03:38 浏览: 329
可以使用以下的 Python 代码实现:
```python
n = int(input("请输入学生的课程数:"))
scores = []
for i in range(n):
score = float(input("请输入第 %d 门课的成绩:" % (i+1)))
scores.append(score)
total_score = sum(scores) # 总分
average_score = total_score / n # 平均分
max_score = max(scores) # 最高分
min_score = min(scores) # 最低分
print("总分为:%.2f" % total_score)
print("平均分为:%.2f" % average_score)
print("最高分为:%.2f" % max_score)
print("最低分为:%.2f" % min_score)
```
这段代码首先通过 `input` 函数获取学生的课程数 `n`,然后使用 `for` 循环输入每门课的成绩,并将其存储在一个列表 `scores` 中。接着,分别使用 Python 内置函数 `sum`、`max` 和 `min` 计算总分、最高分和最低分,并计算平均分。最后,使用 `print` 函数输出结果。
需要注意的是,上述代码中使用了 `%` 运算符和字符串格式化来输出字符串。其中,`%.2f` 表示保留两位小数的浮点数。
相关问题
用Python编写一个自动分辨成绩是否合格并计算最高分,最低分,平均分的函数
好的,以下是一个简单的 Python 函数,可以自动分辨成绩是否合格并计算最高分,最低分,平均分:
```python
def grade_analysis(grades):
pass_num = 0
total_score = 0
max_score = 0
min_score = float('inf')
for score in grades:
total_score += score
if score >= 60:
pass_num += 1
if score > max_score:
max_score = score
if score < min_score:
min_score = score
avg_score = total_score / len(grades)
print("合格人数:", pass_num)
print("最高分:", max_score)
print("最低分:", min_score)
print("平均分:", avg_score)
```
这个函数接受一个列表作为参数,其中包含了所有学生的成绩。函数会遍历这个列表,计算出合格人数,总分,最高分,最低分和平均分,并输出它们的值。注意,这个函数并没有返回任何值,它只是将结果打印出来。如果你需要将这些值用于其他计算,可以修改函数,使其返回一个包含这些值的元组。
用Python编写可以将一组考试成绩通过键盘输入,计算及格率、平均分、最高分和最低分。的代码
好的,我可以回答你关于Python编写的问题。下面是一个示例程序,可以计算输入的成绩的及格率、平均分、最高分和最低分,请注意仔细阅读以下代码,以确保您完全理解:
```
scores = []
total_score = 0
num_scores = 0
pass_count = 0
highest_score = 0
lowest_score = 100
while True:
score = input("请输入学生成绩(输入-1结束):")
score = int(score)
if score == -1:
break
else:
scores.append(score)
total_score += score
num_scores += 1
if score >= 60:
pass_count += 1
if score > highest_score:
highest_score = score
if score < lowest_score:
lowest_score = score
if num_scores == 0:
print("没有输入任何成绩")
else:
avg_score = total_score / num_scores
pass_rate = pass_count / num_scores * 100
print("平均分:", avg_score)
print("及格率:", pass_rate, "%")
print("最高分:", highest_score)
print("最低分:", lowest_score)
```
解释一下代码的具体含义:
首先定义了一个空列表scores,存储输入的成绩;total_score和num_scores分别用于统计总分和个数;pass_count用于统计及格人数;highest_score和lowest_score用于保存最高分和最低分。
进入while循环,使用input函数输入成绩。判断输入的分数是否为-1,如果是则跳出while循环,否则将输入的成绩加入到scores列表中,并将总分、个数等统计量加1。
同时,如果输入的成绩超过60分,则pass_count加1;如果输入的成绩超过最高分,则更新highest_score的值;如果输入的成绩低于最低分,则更新lowest_score的值。
当输入的成绩全部处理完毕,输出平均分、及格率、最高分和最低分等统计结果。
希望你能满意我的回答。