2021-11-28 计算20名学生的平均成绩

一,计算20名学生的平均成绩(防范超出范围的数据)

(1)编写代码

在这里插入图片描述

sum = 0
for i in range(20):
    while True:
        score = int(input('输入第{}个学生的成绩:'.format(i + 1)))
        if 0 <= score <= 100:
            break
        else:
            print('成绩超出范围,必须在[1,100]范围内!')
    sum += score
average = sum / 20
print('平均成绩:{}'.format(round(average, 2)))

(2)运行程序,查看结果

在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/Pythonwudud/article/details/121588977