Home > Back-end >  How do I get the result in the grades list in 4 seperate lists for each class like the marks list
How do I get the result in the grades list in 4 seperate lists for each class like the marks list

Time:12-16

How do I get the result in the grades list in 4 seperate lists for each class like the marks list :

marks = [[87, 91, 48, 53, 65, 79, 57, 85, 94, 82],         # marks of class A
        [61, 73, 37, 85, 45, 85, 92, 18, 18, 89],          # marks of class B
        [87, 94, 35, 65, 84, 64, 25, 61, 46, 28],          # marks of class C
        [56, 19, 55, 77, 52, 84, 47, 32, 88, 68],          # marks of class D
        [75, 95, 58, 52, 65, 77, 65, 62, 91, 45]]          # marks of class E

# Write your code here to complete the challenges given 
grades = []
for i in marks:
    for j in range(len(i)):
        i[j] =5
    for k in i:
            if k <25: 
                t = 'F'
            elif k<=40 :
                t = 'E'
            elif k<=60 :
                t = 'D'
            elif k<=75 :
                t = 'C'
            elif k<90 :
                t = 'B'
            else:
                t = 'A'
            grades.append(t)
   
print(marks) 
print(grades)

I tried to get the grades for eaach student based on their marks in seperate lists for each class but there is no list seperation in the result. Please tell how to get the desired result with minimum lines of code written. Also, is there any way to avoid writing all the if, elif and else statements on so many different lines . can we do something like if k < 25, k<=40, k<=40...: t = 'F','E','D'...

CodePudding user response:

Create a control structure (list of 2-tuples in this case) then it's a case of a few nested loops like this:

from functools import cache

marks = [[87, 91, 48, 53, 65, 79, 57, 85, 94, 82],         # marks of class A
        [61, 73, 37, 85, 45, 85, 92, 18, 18, 89],          # marks of class B
        [87, 94, 35, 65, 84, 64, 25, 61, 46, 28],          # marks of class C
        [56, 19, 55, 77, 52, 84, 47, 32, 88, 68],          # marks of class D
        [75, 95, 58, 52, 65, 77, 65, 62, 91, 45]]

params = [
    (24, 'F'),
    (40, 'E'),
    (60, 'D'),
    (75, 'C'),
    (89, 'B')
    ]

@cache
def get_grade(mark):
    for s, m in params:
        if mark <= s:
            return m
    return 'A'

grades = [[get_grade(mark) for mark in clazz] for clazz in marks]

print(grades)

Output:

[['B', 'A', 'D', 'D', 'C', 'B', 'D', 'B', 'A', 'B'], ['C', 'C', 'E', 'B', 'D', 'B', 'A', 'F', 'F', 'B'], ['B', 'A', 'E', 'C', 'B', 'C', 'E', 'C', 'D', 'E'], ['D', 'F', 'D', 'B', 'D', 'B', 'D', 'E', 'B', 'C'], ['C', 'A', 'D', 'D', 'C', 'B', 'C', 'C', 'A', 'D']]

CodePudding user response:

Your code is fine. There are ways to reduce the number of if/elifs but they will make your code less readable. You should just reset grades when you start processing each class. Make the following changes:

for i in marks:
    grades = []
    ...
        grades.append(t)
    print(grades)

it should then print:

['A', 'A', 'D', 'D', 'C', 'B', 'C', 'A', 'A', 'B']
['C', 'B', 'D', 'A', 'D', 'A', 'A', 'F', 'F', 'A']
['A', 'A', 'E', 'C', 'B', 'C', 'E', 'C', 'D', 'E']
['C', 'F', 'D', 'B', 'D', 'B', 'D', 'E', 'A', 'C']
['B', 'A', 'C', 'D', 'C', 'B', 'C', 'C', 'A', 'D']

CodePudding user response:

I'd suggest making the marks-to-grades conversion a function. Then, you can just call that function in a nested list-comprehension, same for adding 5 to each mark:

def to_grade(mark):
    if mark < 25: 
        return 'F'
    elif mark <= 40 :
        return 'E'
    elif mark <= 60 :
        return 'D'
    elif mark <= 75 :
        return 'C'
    elif mark < 90 :
        return 'B'
    else:
        return 'A'

marks = [[mark   5 for mark in group] for group in marks]
grades = [[to_grade(mark) for mark in group] for group in marks]

If you want to reduce the number of if/elif statements, you could e.g. use a list or dict of mark/grade pairs. Note that you use < in some and <= in other comparisons, so you have to offset those accordingly. (When using a dict, it's important that the dict is iterated in order, which is the case for all newer versions of Python.)

grade_ranges = {90: 'A', 75.1: 'B', 60.1: 'C', 40.1: 'D', 25: 'E', 0: 'F'}
def to_grade(mark):
    return next(g for m, g in grade_ranges.items() if mark >= m)
  • Related