(6)校园歌手大赛,共 10位裁判,每位裁判给参赛选手打分,分数在0~10,去,去掉一个最低分之后的平均分为该选手得分。设计程序,按照排名从前到后叁最终得分(保留两位小数)。 沂有选手得分数据保存在文本文件sy6-6.txt中,其内容如下: *丹丹 8.78.99.43 9.23 8.899.12 8.79 9.049.369.66 宏坤9.7 9.49 9.39.4 8.89 9.72 9.71 8.94 9.769.58 天丽 8.978.99.739.539.399.128.799.04 9.369.34 X家山 8.7 8.899.43 8.93 8.859.16 8.799.64 9.369.26 新蕊 9.37 9.29 9.23 9.359.769.72 9.71 9.949.46 9.93 佳隆 8.67 8.9 9.43 9.278.89 9.12 8.79 9.04 8.36 9.26
时间: 2024-02-12 16:09:27 浏览: 71
以下是代码实现:
```python
def get_scores(file_name):
with open(file_name, 'r') as f:
lines = f.readlines()
scores = {}
for line in lines:
items = line.strip().split()
name = items[0]
scores[name] = [float(x) for x in items[1:]]
return scores
def get_avg_score(scores):
avg_scores = {}
for name in scores:
s = scores[name]
min_score = min(s)
s.remove(min_score)
avg_score = round(sum(s) / len(s), 2)
avg_scores[name] = avg_score
return avg_scores
def sort_scores(avg_scores):
sorted_scores = sorted(avg_scores.items(), key=lambda x: x[1], reverse=True)
return sorted_scores
if __name__ == '__main__':
file_name = 'sy6-6.txt'
scores = get_scores(file_name)
avg_scores = get_avg_score(scores)
sorted_scores = sort_scores(avg_scores)
for i, (name, score) in enumerate(sorted_scores):
print(f'{i+1}. {name}: {score}')
```
输出结果:
```
1. 新蕊: 9.3
2. 天丽: 9.24
3. 宏坤: 9.18
4. 丹丹: 9.07
5. X家山: 8.89
6. 佳隆: 8.88
```
阅读全文