Skip to content
Advertisement

Python grading system using if and for

I have list of name = jimmy, carolin, frank, joseph and their score is = 100 , 90, 70, 65

The grades are :

A = 95 - 100
B = 80 - 94
C = 70 - 79
D = 60 - 69
E = 0 - 59

and I put it like this

students = ["jimmy", "carolin", "frank", "joseph"]

scores = ["100", "90", "70", "65"]

if scores >= 95 and scores <=100: 
    return "A"
elif scores >= 80 and scores <=94: 
    return "B"
elif scores >= 70 and scores <=79: 
    return "C"
elif scores >= 60 and scores <=69: 
    return "D"
else: 
    return "E"

I need to make it like this:

jimmy, 100, A
carolin, 90, B
frank, 70, C
joseph, 65, D

Can some one help me please? Thank you in advance!

Advertisement

Answer

students = ["jimmy", "carolin", "frank", "joseph"]

scores = ["100", "90", "70", "65"]

def function(scores):
    if scores >= 95 and scores <=100:
        return "A"
    elif scores >= 80 and scores <=94:
        return "B"
    elif scores >= 70 and scores <=79:
        return "C"
    elif scores >= 60 and scores <=69:
        return "D"
    else:
        return "E"

for student in students:
    print (student + "," + str(scores[students.index(student)]) + "," + function(int(scores[students.index(student)])))

I added everything in one print statement, hope you can understand. Your if else function is also added into a function for easier use.

For the score, I’m assuming the student’s index would be similar with the score’s index

You should receive the following output:

jimmy,100,A
carolin,90,B
frank,70,C
joseph,65,D
User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement