samedi 11 janvier 2020

If statement to repeat for every occurence

I have a list with different course names followed by a comma then followed by the grade.

courses = ['COMP 1000,A+', "MATH 1000,A+", 'SCIE1000,B+"]

For every time a grade occurs in the list, I change it to the grade point value and add it to a different list. Then from there I calculate the GPA by finding the average. I am using the following code for that:

if any("A+" in n for n in courses):
    grades.append(4.30)
if any("B+" in n for n in courses):
    grades.append(3.30)

This is working fine for lists where each grade is repeated once but for the courses list above there are two A+'s however 4.30 is only being added once. Is there a way to add 4.30 to the new list for every time a grade is in a list?

Thanks

Aucun commentaire:

Enregistrer un commentaire