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.
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!
Related
I'm currently trying to make a program that calculates your final semester grade based on the grades you received previously and how big of an impact they have on your semester grade. The problem is I'm struggling to figure out how you can make the program ask the user for the number of grades they received and then ask for the same amount of grades.
In a nutshell, the program is supposed to do the following:
Ask for the number of received grades
Ask what grade the user got and the percentage of that grade x amounts of times, where x is the number the user gave on the first question.
Calculate the final semester grade based on the inputs.
I hope someone can help, I've made this code so far in case it helps you to understand what I'm trying to do.
grade1 = int(input("What was the first grade you received? " ))
percentage1 = float(input("What's the percentage of the first grade? "))
grade_value1 = grade1 * percentage1
grade2 = int(input("\nWhat was the second grade you received? "))
percentage2 = float(input("What's the percentage of the second grade? "))
grade_value2 = grade2 * percentage2
grade3 = int(input("\nWhat was the third grade you received? " ))
percentage3 = float(input("What's the percentage of the third grade? "))
grade_value3 = grade3 * percentage3
finale_grade = grade_value1 + grade_value2 + grade_value3
if finale_grade < 2.9:
print("\nYour finale grade is 2")
elif finale_grade < 5.4:
print("\nYour finale grade is 4")
elif finale_grade < 8.4:
print("\nYour finale grade is 7")
elif finale_grade < 10.9:
print("\nYour finale grade is 10")
else:
print("\nYour finale grade is 12")
#I hope this works , I'm a beginner tho
num_of_recieved_grades = int(input("number of recieved
grades ?"))
final_grade = 0
# for loop in range of the number entered
for x in range (num_of_recieved_grades ):
grade = int(input("what is the grade"))
percentage = float(input("what is the percentage"))
final_grade += grade * percentage
I've been struggling with this problem for a while now since loops are a bit confusing to me. Essentially, I think I got some of the more difficult stuff (for me at least) out of the way. The issue is that I need the average of 3 students per exam not 6 students per exam, which is what my current code gives.
If what I am asking isn't clear let me know and I will clear it up.
My inputs are n = 3 and m = 2.
def readGrade():
grade = int(input("Enter the grade: "))
while grade > 100 or grade < 0:
print("Invalid Grade")
grade = int(input("Enter the grade: "))
return grade
def examAverage(m):
average = 0
for i in range(n):
readGrade()
average = average + readGrade()
return (average / n)
n = int(input("Enter the number of students: "))
m = int(input("Enter the number of exams: "))
for i in range(m):
print("The average of exam", i + 1, "is:", examAverage(m))
You are calling readGrade() two times within examAverage()
I think this is what examAverage() should be:
def examAverage(m):
average = 0
for i in range(n):
this_grade = readGrade()
average = average + this_grade
return (average / n)
I can't seem to figure this out - I want to get the average of all the inputs at the end of this program but I don't know how to save the inputs with each iteration of the loop. Any help would be much appreciated, thanks!
students = int(input("How many students are in your class?"))
while students <= 0:
print ("Invalid # of students. Try again.")
students = int(input("How many students are in your class?"))
studentnum = 1
for x in range (0, students):
print ("*** Student", studentnum, " ***")
score1 = int(input("Enter score for test #1"))
while score1 < 0:
print ("Invalid score. Try again.")
score1 = int(input("Enter score for test #1"))
score2 = int(input("Enter score for test #2"))
while score1 < 0:
print ("Invalid score. Try again.")
score1 = int(input("Enter score for test #1"))
part1 = score1 + score2
av = part1 / 2
print ("Average score for student #", studentnum, "is", av)
studentnum = studentnum + 1
# figure out how to average out all student inputs
Just create something to store them in outside your loop
scores = []
for x in range (0, students):
...
scores.append({'score1': score1, 'score2': score2, 'avg', av})
total_avg = sum(d['avg'] for d in scores) / len(scores)
You can build up a list of average scores for each student (by appending to it on each loop iteration), and then find the average of those after exiting the loop:
student_average_scores = []
for student in xrange(students):
# <your code that gets av>
student_average_scores.append(av)
average_score = sum(student_average_scores) / float(len(student_average_scores))
I have to write code that will calculate the average of three grades for each of a number of students, and display a message depending on the resulting average grades.
The program needs to be able process any number of students; but each student will have only 3 grades. I have to use a method to calculate the average score for each student, and also to determine the appropriate message depending on the average.
This is the code I have so far and I am stuck:
def main():
more = 'y'
while more == 'y' and more == 'Y':
numScore = int(input('How many test score per student: '))
for numtest in range(numScore):
print ("The score for test")
score = int(input(': '))
total += score
total = 0
print ("Student number", students + 1)
print ('-----------------------------------------')
avg = getAvg(numScore)
if avg > 90:
print ("You're doing excellent work")
elif avg > 85 and avg <= 90:
print("You are doing pretty good work")
elif avg > 70 and avg <= 85:
print ("You better get busy")
else:
print ("You need to get some help")
def getAvg (numScore):
avg = total / numScore
print ("The average for student number", student + 1, \ "is:", avg)
more = input('Do you want to enter another student test score and get the average score of the student (Enter y for yes and n for no): ')
main()
You had most of the elements correct in your script, but you need to think more about the order of everything.
Make sure you pass any arguments that are needed to a function. Your getAvg function needed three arguments for it to calculate the average and display it with the student number. Also you forgot to return average.
When calculating the avg ranges, if you first test for >90, then by definition the next elif avg > 85 will be less than or equal to 90 so it is only necessary to then test for >85.
The following has been rearranged a bit to work:
def getAvg(student, total, numScore):
average = total / numScore
print ("The average for student number", student, "is:", average)
return average
def main():
student = 0
more = 'y'
while more == 'y':
student += 1
total = 0
numScore = int(input('How many test scores per student: '))
for test_number in range(numScore):
print ("The score for test", test_number+1)
score = int(input(': '))
total += score
print ("Student number", student)
print ('-----------------------------------------')
avg = getAvg(student, total, numScore)
if avg > 90:
print ("You're doing excellent work")
elif avg > 85:
print("You are doing pretty good work")
elif avg > 70:
print ("You better get busy")
else:
print ("You need to get some help")
print()
print('Do you want to enter another student test score and get the average score of the student?')
more = input('Enter y for yes, and n for no: ').lower()
main()
Giving you the following kind of output:
How many test scores per student: 3
The score for test 1
: 93
The score for test 2
: 89
The score for test 3
: 73
Student number 1
-----------------------------------------
The average for student number 1 is: 85.0
You better get busy
Do you want to enter another student test score and get the average score of the student?
Enter y for yes, and n for no: n
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))
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))