As my program goes through the for loop, I need the final prompt to include the name of the student with the highest average so far and show that average. As my code is now it shows the highest average so far, but the name of the student keeps changing. How do I get the highest average to stay associated with the appropriate name from the list?
students = ["Kenny", "Irvin", "Ezra", "Joe"]
highest = []
for student in students:
print(student, ", input your 3 grades:")
D = int(input("Discussion: "))
Q = int(input("Quiz: "))
A = int(input("Assignment: "))
wg1 = D * (.20)
wg2 = Q * (.30)
wg3 = A * (.50)
WeightGrade = wg1 wg2 wg3
highest.append(WeightGrade)
print("The average grade for " ,student, " is:", WeightGrade)
print('\n')
max_highest = max(highest)
if highest is None:
print("nothing")
else:
print("The student with the highest grade is:" ,student, "with a"\
,max_highest, "average.")
print('\n')
CodePudding user response:
Change your list's name from highest
to grades
-- it has all the grades, not just the highest one! Then change:
highest.append(WeightGrade)
to:
grades.append((WeightGrade, student))
Now grades
is a list of (grade, student)
tuples. That means you can do:
highest_grade, best_student = max(grades)
print(f"The student with the highest grade is: {best_student} with a {highest_grade} average.\n")
Note that it is not possible for max
to return None
in this situation, so a check like if highest is None:
serves no purpose.
CodePudding user response:
First, your variable names should reflect what the variable represents. In this case, highest
is a bad name because that list contains all grades, so just call it grades
.
Next, to find the maximum of N numbers, you don't calculate the maximum every time you are given a new number! Instead, you want until you have all of them before calculating the max, so do that -- in the loop, just append WeightGrade
to grades
.
students = ["Kenny", "Irvin", "Ezra", "Joe"]
grades = []
for student in students:
print(student, ", input your 3 grades:")
D = int(input("Discussion: "))
Q = int(input("Quiz: "))
A = int(input("Assignment: "))
wg1 = D * (.20)
wg2 = Q * (.30)
wg3 = A * (.50)
WeightGrade = wg1 wg2 wg3
grades.append(WeightGrade)
print("The average grade for " ,student, " is:", WeightGrade)
print('\n')
Then, outside the loop, find the max()
and which students have that grade:
highest = max(grades)
print("The maximum grade is ", highest)
print("The following students achieved this grade: ")
for student, grade in zip(students, grades):
if highest == grade:
print(student)
By having a separate loop that checks if a student's grade is equal to highest
, you cover the case where multiple students have achieved the highest grade.
CodePudding user response:
You just need to create a dictionary that stores the name of the student with the highest average.
I used a max function with two parameters, the averages dict and it checks the max of the keys (which are the averages of the students).
students = ["Kenny", "Irvin", "Ezra", "Joe"]
averages = {}
for student in students:
print(student, ", input your 3 grades:")
D = int(input("Discussion: "))
Q = int(input("Quiz: "))
A = int(input("Assignment: "))
wg1 = D * (.20)
wg2 = Q * (.30)
wg3 = A * (.50)
WeightGrade = wg1 wg2 wg3
averages[student] = WeightGrade
print("The average grade for " ,student, " is:", WeightGrade)
print('\n')
best_student = max(averages, key=averages.get)
if averages is None:
print("nothing")
else:
print("The student with the averages grade is:" ,best_student, "with a"\
,averages[best_student], "average.")
print('\n')