Search code examples
pythonfunctionerror-handlinginstance

is there a way to get the avg score into the grades function without it having an error


I'm relearning python and I decided to make a student grade analyzer program where you enter your grades then it gets the avg of all grades and go to grades function that I made and checks what grades.

# Define Grades 
def grades(score) :
    if score >= 90 and score <= 100:
        print('A')
    elif score >= 80 and score <= 89:
        print('B')
    elif score >= 70 and score <= 79:
        print('C')
    elif score >= 60 and score <= 69:
        print('D')
    else:
        print('F')

def getAvg():
    grades_input = input('Type in your grades no commas ')
    grade_list = grades_input.split()
    scores = []
    i = 0
    for grade in grade_list:
        if ( i < len(grade_list)):
            scores.append(int(grade))
    avg = lambda x: sum(scores) / len(scores)
    return avg(scores)

getAvg()
grades(getAvg)

i tried puting the get avg function inside grades() when I called it I tried putting the scores inside the grades nothing, the getAvg() function works completely fine and proud of it no Ai used but my expextions was that the user puts in grades lets say the grades were 44 53 22 30 and lets say the avg was 78 and like it will go back to the grades function and print out 'C


Solution

  • The main issue is that you are not correctly passing the average score to the grades function. You need to call the grades function with the result of the getAvg function like:

    avg = getAvg()
    grades(avg)
    

    Also, your getAvg function can be optimized further by

    def getAvg():
        grades_input = input('Type in your grades separated by spaces: ')
        grade_list = grades_input.split()
        scores = [int(grade) for grade in grade_list]  # Convert input to list of integers
        avg = sum(scores) / len(scores)  # Calculate average
        return avg