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'...
Your code is fine. There are ways to reduce the number of if
/elif
s 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']