Python_最高分最低分平均分

列表、max()、min()、sum()、len()函数的应用
体会列表:对多个数据进行统一操作!!

"""
    在终端中循环录入学生成绩,如果录入空,则停止,打印最高分,最低分,平均分
"""
grade = []
while True:
    str_score = input('请输入成绩:')
    if str_score == '':
        print("最高分:" + str(max(grade)) + "\n最低分:" + str(min(grade)) + "\n平均分:%.1f" % (sum(grade) / len(grade)))
        break
    else:
        grade.append(float(str_score))
  • 8
    点赞
  • 26
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: 假设有一个列表scores存储了学生的成绩,可以使用以下代码实现去掉最高分最低平均分: ```python scores = [80, 90, 70, 85, 95, 60] # 去掉最高分最低 scores.remove(max(scores)) scores.remove(min(scores)) # 求平均分 avg_score = sum(scores) / len(scores) print(avg_score) ``` 输出结果为: ``` 80.0 ``` 其中,`max(scores)`和`min(scores)`别返回列表中的最高分最低,`remove()`方法可以将列表中的指定元素删除,`sum(scores)`返回列表中所有元素的和,`len(scores)`返回列表中元素的个数。 ### 回答2: 对于这个问题,我们可以使用Python中的一些特定函数和操作来解决。 首先,我们需要收集学生的成绩列表。假设我们已经得到了一个名为scores的成绩列表,可以按照以下步骤进行处理: 1.使用Python内置函数min()和max()找到最低最高分。 ```python lowest = min(scores) highest = max(scores) ``` 2.去掉最低最高分 ```python scores.remove(lowest) scores.remove(highest) ``` 3.计算平均分 ```python average = sum(scores) / len(scores) ``` 最终的代码如下: ```python scores = [90, 85, 92, 88, 94, 80] lowest = min(scores) highest = max(scores) scores.remove(lowest) scores.remove(highest) average = sum(scores) / len(scores) print("最终平均分:", average) ``` 以上就是使用Python去掉最高分最低平均分的整个过程。 ### 回答3: 在Python中,去掉最高分最低平均分可以通过以下步骤实现: 1. 给定一个列表(或数组)包含学生的成绩数据,存储在scores中。 2. 使用Python内置的sort()函数对该列表进行升序排序,这样最高分最低就会排在列表的最后和最前。 3. 通过使用切片操作,去除列表中的第一个元素(即最低)和最后一个元素(即最高分)。这可以通过scores[1:-1]实现。 4. 计算剩下元素的平均值,可以通过使用sum()和len()内置函数实现,即sum(scores[1:-1])/len(scores[1:-1])。 下面是一个示例函数,该函数使用上述方法来计算一个给定成绩列表的平均分数: ```python def average_score(scores): # sort the scores in ascending order scores.sort() # remove the first and last elements (lowest and highest scores) scores = scores[1:-1] # calculate the average score avg_score = sum(scores) / len(scores) return avg_score ``` 使用该函数,我们可以输入一个成绩列表,如[90,85,95,88,92],并输出去掉最高分最低平均分。 ```python >>> scores = [90,85,95,88,92] >>> avg = average_score(scores) >>> print(avg) 90.0 ``` 通过这个函数,我们可以方便地计算任何成绩列表中去掉最高分最低平均分

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值