平均数案例(不用列表与防超出)
作者:互联网
# (1) 不用列表
sum = 0
for i in range(10):
score = int(input('输入第{}个学生成绩:'.format(i + 1)))
sum = sum + i
avg = int(sum / 10)
print('平均成绩:{}'.format(avg))
# (2)防范超出
sum = 0
for i in range(10):
while True:
score = int(input('输入第{}个学生成绩:'.format(i + 1)))
if 0 <= score <= 100:
break
else:
print('成绩超出范围,请输入[0-100]的数')
sum = sum + score
avg = int(sum / 10)
print('平均成绩:{}'.format(avg))
标签:10,format,int,平均数,sum,列表,案例,range,score 来源: https://blog.csdn.net/Argonaut_/article/details/121407523