Answer:
As requested here is a beginner version of the presented code.
names = ["Sam", "Fred", "Juan", "Drake", "Mervat"]
subjects = ["Math", "Science", "English"]
averages = []
for name in names:
grades = []
for subject in subjects:
while True:
try:
grade = float(input("Please enter " + name + "'s grade for " + subject + ": "))
if grade < 0:
raise ValueError
break
except ValueError:
print("Invalid input. Please enter a non-negative grade.")
grades.append(grade)
average = sum(grades) / len(grades)
averages.append((name, average))
highest_average = max(averages, key=lambda x: x[1])[1]
highest_students = [entry for entry in averages if entry[1] == highest_average]
print("\\Highest average grade(s):")
for student, average in highest_students:
print(student + ": " + "{:.2f}".format(average))
I have removed the functions and inlined the code within the main loops, and I also replaced f-strings with string concatenation
def get_grade_input(name, subject):
while True:
try:
grade = float(input(f"Please enter {name}'s grade for {subject}: "))
if grade < 0:
raise ValueError
return grade
except ValueError:
print("Invalid input. Please enter a non-negative grade.")
def calculate_average(grades):
return sum(grades) / len(grades)
names = ["Sam", "Fred", "Juan", "Drake", "Mervat"]
subjects = ["Math", "Science", "English"]
averages = []
for name in names:
grades = []
for subject in subjects:
grade = get_grade_input(name, subject)
grades.append(grade)
average = calculate_average(grades)
averages.append((name, average))
highest_average = max(averages, key=lambda x: x[1])[1]
highest_students = [entry for entry in averages if entry[1] == highest_average]
print("\\Highest average grade(s):")
for student, average in highest_students:
print(f"{student}: {average:.2f}")
Step-by-step explanation:
Here is how all of this cohesively works together in an ordered format. You can use any compiler online or compile it yourself and it will work. I have already compiled it myself to confirm it works.
- Create a function called get grade input(name, subject) that accepts these two inputs: a student's name and a subject. The task of this function is to ask the user to enter a grade for the specified student and topic, to validate the input, and to return a legitimate grade.
- Create a function called calculate average(grades) that receives a list of grades as input, computes the average, and delivers the outcome.
- Make a list of the students' names and a list of their subjects.
- Create an empty list called averages and fill it with the names of all the students and their average grades to save as a tuple.
- For each student's name and subject combination, use nested for loops to cycle over them. Use the get grade input(name, subject) function to ask the user to provide a grade for each combination. Make a list titled grades with all the grades.
- To determine the average grade for a student, call the function calculate average(grades) after gathering all of the student's grades.
- Add a tuple with the student's name and average grade to the list of averages.
- Using a custom key function and the max() function, find the student with the highest average grade.
- A list comprehension can be used to compile a list of the students with the highest average grade. If a student's average grade is greater than the highest average grade, this list will only hold tuples with their name and average grade.
- Iterate through the highest students list, printing the highest average grade(s) and the student name(s) that correspond.