Need to use Max to get the largest grade of four students

Question:

I just cant figure out to use the max command in this program, at this point it keeps telling me that a float value is not iterable

def main():
    students = ["Mike", "John", "James", "Dan"]

    disGrade = .10
    quizGrade = .30
    assignGrade = .60

    def grade_math(a,b,c):
        disGradeFin=disGrade*a
        quizGradeFin=quizGrade*b
        assignGradeFin=assignGrade*c
        averageGrade=disGradeFin+quizGradeFin+assignGradeFin
        print(student, "final grade is", averageGrade)
        return averageGrade
        averageGrade = grade_math

        #print(grade_math(100,100,100))

    
    for student in students:
            print("Enter", student, "grades")
            a=float(input("Please enter student discussion grade."))
            b=float(input("Please enter student quiz grade."))
            c=float(input("Please enter student assignment grade."))
            grade_math(a,b,c)

            largestGrade=max(grade_math(a,b,c))

main()
Asked By: Death

||

Answers:

Instead of returning the average grade we return the grades itself

def main():
    students = ["Mike", "John", "James", "Dan"]

    disGrade = .10
    quizGrade = .30
    assignGrade = .60

    def grade_math(a,b,c):
        disGradeFin=disGrade*a
        quizGradeFin=quizGrade*b
        assignGradeFin=assignGrade*c
        averageGrade=disGradeFin+quizGradeFin+assignGradeFin
        print(student, "final grade is", averageGrade)
        return disGradeFin, quizGradeFin, assignGradeFin # This is line that changed
        averageGrade = grade_math

        #print(grade_math(100,100,100))

    
    for student in students:
            print("Enter", student, "grades")
            a=float(input("Please enter student discussion grade."))
            b=float(input("Please enter student quiz grade."))
            c=float(input("Please enter student assignment grade."))
            grade_math(a,b,c)

            largestGrade=max(grade_math(a,b,c))

main()
Answered By: Alpha Wolf Gamer

The biggest problem in your code is that your grade_math() function returns a float value, which is a number but not a list or any containers. max() function returns the biggest value in an iterable, but clearly a single number isn’t iterable. I guess you want to pick the highiest grade from all students, so I have an idea. Since float isn’t iterable, you can put the average grade of each student in a list after one loop. When the whole loop is finished, use max() to get the biggest element in that list. Here’s the code:

def main():

    students = ["Mike", "John", "James", "Dan"]
    students_final_grades = []

    disGrade = .10
    quizGrade = .30
    assignGrade = .60

    def grade_math(dis_g, quiz_g, assign_g):

        dis_grade_fin = disGrade * dis_g
        quiz_grade_fin = quizGrade * quiz_g
        assign_grade_fin = assignGrade * assign_g
        average_grade = dis_grade_fin + quiz_grade_fin + assign_grade_fin
        print(student, "final grade is", average_grade)
        return average_grade
        average_grade = grade_math

    
    for student in students:

        print("Enter", student, "grades")
        dis_g = float(input("Please enter student discussion grade."))
        quiz_g = float(input("Please enter student quiz grade."))
        assign_g = float(input("Please enter student assignment grade."))
        students_final_grades.append(grade_math(dis_g, quiz_g, assign_g))  # The code changed

    largest_grade = max(students_final_grades)
    print(f'The highiest grade of all is:{largest_grade}')


main()

This is one simple solution. However, this code can be optimized more using dict. Change the two lists declared in main() to:

student_name_and_grade = {"Mike":0, "John":0, "James":0, "Dan":0}

and change the append() code in the for loop to:

student_name_and_grade[student] = grade_math(dis_g, quiz_g, assign_g)

after that, change next line to:

largest_grade = max(student_name_and_grade.values())

At last, remember to follow the PEP8 Style Guide when coding 🙂

Answered By: Coder
Categories: questions Tags: , , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.