Doug - 7 months ago 32

Python Question

I am the given the following problem and asked to write a solution algorithm for it using python.

problem:

Write a Python program to determine the student with the highest average. Each student takes a midterm and a final. The grades should be validated to be between 0 and 100 inclusive. Input each student’s name and grades and calculate the student’s average. Output the name of the student with the best average and their average.

Here is my code:

`def midTerm():`

midtermScore = int(input("What is the midterm Score: "))

while (midtermScore <= 0 or midtermScore >= 100):

midtermScore = int(input("Please enter a number between 0 and 100: "))

return midtermScore

def final():

finalScore = int(input("What is the final Score: "))

while (finalScore < 0 or finalScore > 100):

finalScore = int(input("Please enter a number between 0 and 100: "))

return finalScore

total = 0

highest = 0

numStudents = int (input("How Many Students are there? "))

while numStudents < 0 or numStudents > 100:

numStudents = int (input("Please enter a number between 0 and 100? "))

for i in range (1, numStudents+1):

students = (input("Enter Student's Name Please: "))

score = (midTerm()+ final())

total += score

avg = total/numStudents

if (highest < avg):

highest = avg

winner = students

print ("The Student with the higgest average is: ", winner, "With the highest average of: ", avg)

The issue I am running into is the last part. The program does not print the name of the person with the highest average, but the name of the person that was entered at the very last. I am very confused on how to go forward from here. Can you please help? Thanks in advance for any help.

Answer

You're not far off. Take a look here:

```
for i in range (1, numStudents+1):
students = (input("Enter Student's Name Please: "))
score = (midTerm()+ final())
total += score
avg = total/numStudents
if (highest < avg):
highest = avg
winner = students
```

Besides the indentation error (hopefully just clumsy copy-pasting) You're not actually calculating each student's average score anywhere. Try something like this:

```
for i in range (numStudents):
student_name = (input("Enter Student's Name Please: "))
student_avg = (midTerm() + final()) / 2 # 2 scores, summed and divided by 2 is their average score
if (highest < student_avg):
highest = student_avg
winner = student_name # save student name for later
print ("The Student with the higgest average is: ", winner, "With the highest average of: ", highest)
```

It looks like you were originally trying to calculate the total class average, which is not what's described by the problem statement. Hope this helps!

Source (Stackoverflow)