输入8个学生的物联网导论期末考试成绩存入到一维数组中,完成以下操作:(1)找出第几位学生的成绩最高,最高是多少?(2)找出第几位学生的成绩最低,最高是多少?(3)按照从小到大的顺序对所有成绩进行排序

scores = [] # 声明空列表
for i in range(8):
    score = int(input("请输入第%d位学生的成绩:" % (i+1))) # 依次输入每个学生的成绩
    scores.append(score) # 将成绩添加到列表中

# 找出最高分和对应的学生编号
max_score = scores[0] # 设第1个学生的成绩为最高分
max_index = 0 # 最高分对应的学生编号
for i in range(1, 8):
    if scores[i] > max_score:
        max_score = scores[i]
        max_index = i

# 找出最低分和对应的学生编号
min_score = scores[0] # 设第1个学生的成绩为最低分
min_index = 0 # 最低分对应的学生编号
for i in range(1, 8):
    if scores[i] < min_score:
        min_score = scores[i]
        min_index = i

# 对成绩进行排序
scores.sort()

# 输出结果
print("最高分是第%d位学生的成绩:%d" % (max_index+1, max_score))
print("最低分是第%d位学生的成绩:%d" % (min_index+1, min_score))
print("按照从小到大的顺序排列后的成绩:", scores)
 

        注意,该代码中使用了input()函数来接收用户输入,因此在运行程序时需要手动输入8个学生的成绩。如果要实现从文件或其他数据源中读取成绩数据,需要修改代码以适应不同的数据输入方式。

猜你喜欢

转载自blog.csdn.net/SYC20110120/article/details/132916742