List collection sum case

Case 01: There are five students [Zhang San, Li Si, Wang Wu, Zhao Liu, Ma Qi], each student has five subjects [Chinese, Math, English, Physics, Chemistry], randomly generate 5 for these 5 students The result of the door examination [score between 50-100]

Requirement: Print out the details of the grades in reverse order of the average score 

 

import random

# student_name stores the student name

student_name = [ " Zhang San " , " Li Si " , " Wang Wu " , " Zhao Liu " , " Ma Qi " ]

# student_result stores all student grade details

student_result = []

#Use a loop to generate the grades for each subject for each person

for i in range(len(student_name)):

    list_temp = []  # grades of 5 subjects

    for j in range(5):

        list_temp.append(random.randint( 50 , 100 )) #one grade at a time

#Insert the grades of 5 subjects into student_result

    student_result.append(list_temp)

 

# Count the total score of each person

student_total_result = [] #Store the total score of each person

for i in student_result:

    student_total_result.append(sum(i))

 

#output details of grades

print ( " rank name Chinese mathematics English physical chemistry total score average " )

print("==================================================================")

for i in range(len(student_name)):

    max_result = max(student_total_result) # 获取最高分分数

    max_index = student_total_result.index(max_result) # 获取最高分的编号

    # 打印-名次

    print(i+1, end="\t\t")

    # 打印-姓名

    print(student_name[max_index], end="\t")

    # 打印-成绩5

    for i in range(5):

        print(student_result[max_index][i], end="\t  ")

    # 打印-总分

    print(max_result, end="\t ")

    # 打印-均分

    print(max_result/5)

    # 删除信息-总分

    student_total_result.pop(max_index)

    # 删除信息-成绩明细

    student_result.pop(max_index)

    # 删除信息-姓名

    student_name.pop(max_index)

print("==================================================================")

执行结果:

C:\python\python.exe C:/python/demo/file2.py

名次    姓名   语文  数学    英语    物理    化学    总分    均分

==================================================================

1                张三        93          79          74          99          96          441         88.2

2                赵六        80          87          99          52          77          395         79.0

3                李四        64          66          82          87          80          379         75.8

4                马七        81          91          71          72          53          368         73.6

5                王五        59          57          56          60          99          331         66.2

==================================================================

 

Process finished with exit code 0


Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324984403&siteId=291194637