计算成绩并分配给每个学生

2024-04-19 10:29:33 发布

您现在位置:Python中文网/ 问答频道 /正文

我在哪里犯了错误?因为这个项目应该只为每个学生取一个名字和一个分数。 练习-给出一份学生名单和他们在本学期课程中取得的分数,计算他们的最终成绩,并根据成绩表将其打印到屏幕上:


list_students = ["Alice", "Bernard", "Charles", "Daniel", "Elisa",
                 "Fabian", "Gabrielle", "Helga", "Ilse", "Johann"]
list_points = [86, 73, 56, 79, 48, 98, 95, 40, 81, 88]

for x in list_students:
     for i in range(len(list_points)): 
        if list_points[i] < 50:
            i = 5
        elif list_points[i] < 63:
            i = 4
        elif list_points[i] < 75:
            i = 3
        elif list_points[i] < 87:
            i = 2
        elif list_points[i] < 100: 
            i = 1
        print(str(x) + ": " + str(i))


Tags: 项目infor错误名字学生分数points
2条回答

你可能需要这样的东西:

for i in range(len(list_points)): 
    if list_points[i] < 50:
        grade = 'E'
    elif list_points[i] < 63:
        grade = 'D'
    elif list_points[i] < 75:
        grade = 'C'
    elif list_points[i] < 87:
        grade = 'B'
    elif list_points[i] < 100: 
        grade = 'A'
    print(list_students[i] + ": " + grade)

OP:每个学生的名字和他/她各自的年级:学生的名字\u 1:年级\u 1

使用^{}

list_students = ['Alice', 'Bernand', 'Charles', 'Daniel', 'Elisa',
                 'Fabian', 'Gabrielle', 'Helga', 'Ilse', 'Johann']
list_points = [86, 73, 56, 79, 48, 98, 95, 40, 81, 88]

for student, point in zip(list_students, list_points):
    print(student, ':', point)

输出:

Alice : 86                                                                                                                                                                   
Bernand : 73                                                                                                                                                                 
Charles : 56                                                                                                                                                                 
Daniel : 79                                                                                                                                                                  
Elisa : 48                                                                                                                                                                   
Fabian : 98                                                                                                                                                                  
Gabrielle : 95                                                                                                                                                               
Helga : 40                                                                                                                                                                   
Ilse : 81                                                                                                                                                                    
Johann : 88

编辑

考虑打印等级:

list_students = ['Alice', 'Bernand', 'Charles', 'Daniel', 'Elisa', 'Fabian', 'Gabrielle', 'Helga', 'Ilse', 'Johann']
list_points = [86, 73, 56, 79, 48, 98, 95, 40, 81, 88]
grades = []

for point in list_points:
    if point <= 100 and point >= 87:
        grades.append('A')
    elif point < 87 and point >= 75:
        grades.append('B')
    elif point < 75 and point >= 63:
        grades.append('C')
    elif point < 60 and point >= 50:
        grades.append('D')
    elif point < 50:
        grades.append('E')


for student, point, grade in zip(list_students, list_points, grades):
    print(student, ':', point, ':', grade)

输出

Alice : 86 : B                                                                                                                                                               
Bernand : 73 : C                                                                                                                                                             
Charles : 56 : D                                                                                                                                                             
Daniel : 79 : B                                                                                                                                                              
Elisa : 48 : E                                                                                                                                                               
Fabian : 98 : A                                                                                                                                                              
Gabrielle : 95 : A                                                                                                                                                           
Helga : 40 : E                                                                                                                                                               
Ilse : 81 : B                                                                                                                                                                
Johann : 88 : A   

相关问题 更多 >