How to return grade as list

Viewed 583

I have a list of items. I wanted to calculate the average scores and return grade values.

I wanted to Compare New_list value and return a list.Is there any simple way of doing it

lst = [[90,80,70,60], [10,81,50, 65]]
Lst_New=[]
New_list=list(map(lambda x:sum(x)/len(x), lst))
print(New_list)


if New_list[0]>90:
    New_grade='A'
elif New_list[0]>=80 and New_list[0]<=90:
    New_grade='B'
elif New_list[0]>=70 and New_list[0]<=80:
    New_grade='C'
elif New_list[0]>=60 and New_list[0]<=70:
    New_grade='D'
else:
    New_grade='F'

if New_list[1]>90:
    New_grade1='A'
elif New_list[1]>=80 and New_list[1]<=90:
    New_grade1='B'
elif New_list[1]>=70 and New_list[1]<=80:
    New_grade1='C'
elif New_list[1]>=60 and New_list[1]<=70:
    New_grade1='D'
else:
    New_grade1='F'

Lst_New.append(New_grade)
Lst_New.append(New_grade1)
print(Lst_New)
3 Answers

Everything you have tried so far is fine, you can just iterate New_List in a loop and include your if, elif block inside the loop, you don't need to define multiple variables, and use redundant codes:

for score in New_list:
    if score > 90:
        New_grade = 'A'
    elif score >= 80 and score <= 90:
        New_grade = 'B'
    elif score >= 70 and score <= 80:
        New_grade = 'C'
    elif score >= 60 and score <= 70:
        New_grade = 'D'
    else:
        New_grade = 'F'
    Lst_New.append(New_grade)

You can go to the Python Control Flow Documentaion and learn about available control flows.

Improved version

Here is an improved version of your code, user3551354. It accomplishes exactly the same result (with regards to the contents of Lst_New), as your original code.

def grade_to_letter(grade):
    if grade > 90: return 'A'
    if grade > 80: return 'B'
    if grade > 70: return 'C'
    if grade > 60: return 'D'
    return 'F'

lst = [[90,80,70,60], [10,81,50, 65]]
Lst_New=[]

for student in lst:
    avg = sum(student) / len(student)
    print(avg)
    
    New_grade = grade_to_letter(avg)
    Lst_New.append(New_grade)

print(Lst_New)

An even more improved version

I took the liberty to rewrite my first version of the program to follow some of the best industry standards, such as proper variable naming (following a convention using underscores), avoiding redundant code, and avoiding redundant variables by utilizing dictionaries. I also added comments, which is the golden industry standard when it comes to designing excellent code.

I hope this example (or revision of your program) helps you!

# Helper function: calculate a letter grade based on a given average score
def grade_to_letter(grade):
    if grade > 90: return 'A'
    if grade > 80: return 'B'
    if grade > 70: return 'C'
    if grade > 60: return 'D'
    return 'F'

# Define the list of students, 
# where each element of the list is a dictionary representing a single student
students = [
    {
        'scores': [ 90, 80, 70, 60 ],
        'average_score': None,
        'letter_grade': None
    },
    {
        'scores': [ 10, 81, 50, 65 ],
        'average_score': None,
        'letter_grade': None
    }
]

# Iterate over the students list
for student in students:
    avg = sum(student['scores']) / len(student['scores'])
    # Update the score and grade values in the dictionary (i.e., the student)
    student['average_score'] = avg
    student['letter_grade'] = grade_to_letter(avg)

# Now the `students` dictionary contains the updated averages and letter grades
print(students)

You could define a method which calculates the average of the grade in a list and then convert that average to the grade.

Here I'm mapping grades into a dictionary with a default value of 'F'.

from collections import defaultdict

def average_grade(grades):
    dct_grades = {60: 'D', 70: 'C', 80: 'B', 90: 'A'}
    dct_grades = defaultdict(lambda: 'F', dct_grades)
    avg = sum(grades) / len(grades)
    # print(avg)
    return dct_grades[int(avg)//10*10]

Then you could just use a list comprehension to map the list containing the statistics:

lst = [[90,80,70,60], [10,81,50,65]]
[average_grade(grades) for grades in lst]
#=> ['C', 'F']

Please consider that dct_grades[100] #=> 'F', so maybe you need to add 100: 'A' to the dictionary.

Related