Reputation: 401
I'm writing a program in python to allow the user to enter the number of students in class, and then 3 test grades for each student. It also needs to show the student's test average, the class average, and the max and min average in the class. Right now, I'm having trouble making it not print the class average after each student's grades and average is printed. I also really can't make the max and mins work because it is changing with each student to that student's average.
students=int(input('Please enter the number of students in the class: '))
for number in range(students):
class_average == 0
first_grade=int(input("Enter student's first grade: "))
second_grade=int(input("Enter student's second grade: "))
third_grade=int(input("Enter student's third grade: "))
StudentAverage=(first_grade + second_grade + third_grade)/3
print("The student's average is", round(StudentAverage,2))
class_average= class_average + StudentAverage
print("The class average is", round(class_average/students,2))
maximum_num = 0
if StudentAverage > maximum_num:
maximum= StudentAverage
print("The maxiumum average is", round(maximum,2))
minimum_num = 100
if StudentAverage < minimum_num:
minimum= StudentAverage
print("The minimum average is", round(minimum,2))
Upvotes: 0
Views: 1898
Reputation: 831
I moved your initializers outside the loop so the value would not reset during each iteration. I moved the maximum and minimum comparisons into the loop and and replaced the maximum and minimum variable. Each new value was less than and greater than those values, respectively, so maximum_num and minimum_num needed to be used instead. The running class average was too low because it used the total number of students instead of the current number calculated. I replaced the use of student with number+1. I think this is the code you want.
students=int(input('Please enter the number of students in the class: '))
class_average = 0
maximum_num = 0
minimum_num = 100
for number in range(students):
first_grade=int(input("Enter student's first grade: "))
second_grade=int(input("Enter student's second grade: "))
third_grade=int(input("Enter student's third grade: "))
StudentAverage=(first_grade + second_grade + third_grade)/3
print("The student's average is", round(StudentAverage,2))
class_average= class_average + StudentAverage
print("The class average is", round(class_average/(number+1),2))
if StudentAverage > maximum_num:
maximum_num = StudentAverage
if StudentAverage < minimum_num:
minimum_num = StudentAverage
print("The minimum average is", round(minimum_num,2))
print("The maxiumum average is", round(maximum_num,2))
Upvotes: 2