模拟最终成绩计算过程

时间: 2023-12-16 admin 维修知识

模拟最终成绩计算过程

模拟最终成绩计算过程

首先输入大于2的整数作为评委人数,然后依次输入每个评委的打分,要求每个分数介于0~100.输入完所有评委打分之后,去掉一个最高分,去掉一个最低分,剩余分数的平均分即为该选手的最终得分

(1)

while True:try:n = int(input('请输入评委人数:'))assert n > 2# 跳出循环breakexcept:print('必须输入大于2的整数')# 用来保存所有评委的打分
scores = []# 依次输入每个评委的打分
for i in range(n):# 这个循环用来保证用户必须输入0~100的数字while True:try:score = float(input('请输入第{0}个评委的分数:'.format(i+1)))assert 0 <= score <= 100scores.append(score)breakexcept:print('必须属于0到100之间的实数.')
# 计算并删除最高分和最低分
highest = max(scores)
scores.remove(highest)
lowest = min(scores)
scores.remove(lowest)
# 计算平均分,保留2位小数
average = round(sum(scores)/len(scores), 2)formatter = '去掉一个最高分{0}\n去掉一个最低分{1}\n最后得分{2}'
print(formatter.format(highest, lowest, average))

(2)

while True:try:n = int(input('请输入评委人数:'))assert n > 2breakexcept:print('必须输入大于2的整数')maxScore, minScore = 0, 100
total = 0
for i in range(n):while True:try:score = float(input('请输入第{0}个评委的分数:'.format(i+1)))assert 0 <= score <= 100breakexcept:print('必须属于0到100之间的实数.')total += scoreif score > maxScore:maxScore = scoreif score < minScore:minScore = score
average = round((total-maxScore-minScore)/(n-2), 2)
formatter = '去掉一个最高分{0}\n去掉一个最低分{1}\n最后得分{2}'
print(formatter.format(maxScore, minScore, average))