Python Forum
Simple Method to calculate average and grade
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Simple Method to calculate average and grade
#1
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
Reply
#2
(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!
jefsummers likes this post
Reply
#3
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)
Reply
#4
you can return both using one return with each returned item separated by a comma
return students_avg, New_grade
Reply
#5
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
Reply
#6
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")
Reply
#7
(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
Reply
#8
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/
Almost dead, but too lazy to die: https://sourceserver.info
All humans together. We don't need politicians!
Reply
#9
I want to create a list for 10 students with 3 courses per students and display their grades in a similar way, how?
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Non Grade/School Help - PLEASE gbyrne12 8 2,982 Jun-19-2021, 07:31 PM
Last Post: snippsat
  How to calculate the lexical diversity average (with 1000 window word length) AOCL1234 6 3,339 Jul-27-2020, 06:16 AM
Last Post: DPaul
  Calculating Grade Average IstvanCH 5 4,951 Jan-27-2019, 04:42 PM
Last Post: aakashjha001
  Student grade program help debug ccm1776 3 5,039 Nov-14-2018, 02:41 AM
Last Post: stullis
  Grade Loop dtweaponx 8 12,254 Oct-17-2017, 02:01 PM
Last Post: buran
  Help? Letter Grade assignment.. zepel 3 4,537 Apr-23-2017, 12:47 PM
Last Post: idontreallywolf

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020