Python Forum

Full Version: Simple Method to calculate average and grade
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Dear all

I have list of items
a=[[90 90 80 87 85],[60 70 80 7 85]] having list of item in as above arrange in 2 row and 5 column ist

I wanted to calculate grade using input. First need to take average and then average must be compare between range to return grade value

if average> 90 grade must be A
if average> 80 and <=90 grade must be B
if average> 70 and <=80 grade must be C
else
grade must be F

can some one guide me here . simple way of doing it
(Jun-07-2021, 12:33 PM)ajitnayak1987 Wrote: [ -> ]Dear all

I have list of items
a=[[90 90 80 87 85],[60 70 80 7 85]] having list of item in as above arrange in 2 row and 5 column ist

I wanted to calculate grade using input. First need to take average and then average must be compare between range to return grade value

if average> 90 grade must be A
if average> 80 and <=90 grade must be B
if average> 70 and <=80 grade must be C
else
grade must be F

can some one guide me here . simple way of doing it
Well, no. This is clearly homework. If I answered it for you, you might get an A, but would have deserved an F. Work it out yourself, and you will earn that A.

Hint: use a loop. One loop sums up the elements and counts them. This gives you the average. The rest are simple if-statements. Note that if these two sublists represent two students, then you will need nested loops. One to iterate over the main list, and another to iterate over each of the sublists and work out the average and grade. Converting the grade to a letter grade is a great opportunity to use def to create a function that does that.

And that's as far as I go. The rest is up to you. Get that A!
I have tried like this . But i wanted to return grade for both student_average

import numpy
myList=[[85,80,98,94],[87,9,75,60]]
New_grade=[]

def print_student_average(myList):
    students_avg = numpy.mean(myList, axis=1)
    for avg in students_avg:
        if(avg>=90):
            New_grade='A'
        elif avg>=80 and avg<=90:
            New_grade='B'
        elif avg>=70 and avg<=80:
            New_grade='C'
        elif avg>=60 and avg<=70:
            New_grade='D'
        else:
            New_grade='F'
            
        print(avg)
    #return students_avg
    return New_grade

Average=print_student_average(myList)
print(Average)
you can return both using one return with each returned item separated by a comma
return students_avg, New_grade
score = [[90, 90, 80, 87, 85], [60, 70, 80, 7, 85]]
average = [sum(list(col))/2 for col in zip(*score)]

for item in average:
    if item > 90: print(f"{item}: grade A")
    if item > 80: print(f"{item}: grade B")
    if item > 70: print(f"{item}: grade C")
    else: print(f"{item}: grade F")
Output:
75.0: grade C 80.0: grade C 80.0: grade C 47.0: grade F 85.0: grade B 85.0: grade C
naughtyCat, your letter grade code does not work.:
average = [95]

for item in average:
    if item > 90: print(f"{item}: grade A")
    if item > 80: print(f"{item}: grade B")
    if item > 70: print(f"{item}: grade C")
    else: print(f"{item}: grade F")
Output:
95: grade A 95: grade B 95: grade C
I thought the output looked odd and realized there were more grades reported in the output than there were grades in the score lists.

You could do this:
for item in average:
    if item > 90: print(f"{item}: grade A"); continue
    if item > 80: print(f"{item}: grade B"); continue
    if item > 70: print(f"{item}: grade C"); continue
    print(f"{item}: grade F")
But that having multiple statements on one line is frowned upon.

This is better, but still frowned upon.
for item in average:
    if item > 90: print(f"{item}: grade A")  # Still multiple statements on one line
    elif item > 80: print(f"{item}: grade B")
    elif item > 70: print(f"{item}: grade C")
    else: print(f"{item}: grade F")
(Aug-26-2021, 05:24 PM)deanhystad Wrote: [ -> ]ya, you are right, because i never check my code just now, small mistakes I shouldn't make

the code should be like this:
average = [95]
 
for item in average:
    if item > 90: print(f"{item}: grade A")
    elif item > 80: print(f"{item}: grade B")
    elif item > 70: print(f"{item}: grade C")
    else: print(f"{item}: grade F")
Output:
95: grade A
You can use the bisect module, to get the index-position for a value to insert it in an ordered list (small to big values).


import bisect


def get_grade(value):
    grades_values = (70, 80, 90)
    # bisect searches from small to biger values
    
    grades_chars = "FCBA"
    # reversed order for grade values
    # D E does not exist?
    
    index = bisect.bisect_right(grades_values, value)
    # index can be len(grade_values) + 1
    return grades_chars[index]
Detailed informations: https://realpython.com/binary-search-python/
I want to create a list for 10 students with 3 courses per students and display their grades in a similar way, how?