2. 随机生成100个学生的成绩保存在文件scores.txt文件中;2). 从文件中读出学生的成绩,输出最高、最低,以及平均成绩(平均成绩为小数)。

时间: 2023-07-29 admin 互联网

2. 随机生成100个学生的成绩保存在文件scores.txt文件中;  	2). 从文件中读出学生的成绩,输出最高、最低,以及平均成绩(平均成绩为小数)。

2. 随机生成100个学生的成绩保存在文件scores.txt文件中; 2). 从文件中读出学生的成绩,输出最高、最低,以及平均成绩(平均成绩为小数)。

  1. 随机生成100个学生的成绩保存在文件scores.txt文件中;
    1). 从文件中读出学生的成绩,输出最高、最低,以及平均成绩(平均成绩为小数)。
    2). 从文件中读出学生的成绩,统计90分以上学生的人数并输出。
    3). 求出这次考试的平均成绩,并统计各分数段的人数(优秀:≥90,良好:≥80,中等:≥70,及格:≥60,不及格:<60)。
    4). 将统计结果保存到数据文件statictic.txt中

    import random
    with open(‘scores.txt’,‘w+’) as f:
    with open(‘statictic.txt’,‘w+’) as f1:
    for i in range(100):
    score = str(random.randint(1,100)) + (’\n’)
    f.write(score)
    f.seek(0,0)
    a = f.readlines()
    li = []
    for i in a:
    i = int(i.strip(’\n’))
    li.append(i)
    li.sort()
    min = li[0]
    max = li[99]
    res = 0
    avg = 0
    n = 0
    k = 0
    m = 0
    s = 0
    l = 0
    d = 0
    for i in li:
    res = res + i
    if i > 90:
    n += 1
    for i in li:
    if i >= 90:
    k += 1
    elif i >= 80 and i < 90:
    m +=1
    elif i >= 70 and i < 80:
    s +=1
    elif i >= 60 and i < 70:
    l +=1
    else:
    d +=1
    avg = res/100
    f1.write(‘最低成绩为%s,最高成绩为%s,平均成绩为%s,大于90分以上的人数为%s,优秀:>=90的人数为%s,良好:>=80的人数为%s,中等:>=70的人数为%s,及格:>=60的人数为%s,不及格:<60的人数为%s’%(min,max,avg,n,k,m,s,l,d))