75.8k views
3 votes
Write a program that asks the user to enter the grades of five students (named Sam, fred, Juan, Drake, and Mervat) in three different subjects (named Math, Science, and English). Your program should use for loops to prompt the user for each grade and then calculate the average grade for each student.

Next, use nested conditional statements to determine which student has the highest average grade, and print out their name and average grade. If two or more students have the same highest average grade, print out all their names. Make sure your program is user-friendly and displays appropriate messages for invalid inputs (e.g. if the user enters a negative grade).

Note: You can use variables to store the grades and averages for each student, instead of using lists. For example, you could use gradeSam_math, gradeSam_science, and gradeSam_english to store the grades for student A, and avgTom to store their average grade.

User Debu
by
8.0k points

1 Answer

7 votes

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.

  1. 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.
  2. Create a function called calculate average(grades) that receives a list of grades as input, computes the average, and delivers the outcome.
  3. Make a list of the students' names and a list of their subjects.
  4. 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.
  5. 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.
  6. To determine the average grade for a student, call the function calculate average(grades) after gathering all of the student's grades.
  7. Add a tuple with the student's name and average grade to the list of averages.
  8. Using a custom key function and the max() function, find the student with the highest average grade.
  9. 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.
  10. Iterate through the highest students list, printing the highest average grade(s) and the student name(s) that correspond.
User Andreister
by
7.7k points