Reputation: 1
I am writing a python program to calculate the test grade of 5 students and it will show what grade they get by entering their test score. But I can't get a result like this. Can someone help me out, please! 5 88 92 76 100 66 Grade Distribution A: 2 B: 1 C: 1 D: 1 F: 0 Average Grade: 84.4
`def read_test_scores():
print("Enter number of students: ")
num = int(input())
print("Enter the next grade: ")
score1 = int(input())
score2 = int(input())
score3 = int(input())
score4 = int(input())
score5 = int(input())
sum = (score)
tavge = sum / 5.0
return tavge
def get_letter_grade():
if 90 <= n <= 100:
grade = 'A'
elif 80 <= n <= 89:
grade = 'B'
elif 70 <= n <= 79:
grade = 'C'
elif 60 <= n <= 69:
grade = 'D'
elif n<60:
grade = 'F'
return grade
def print_comment(grade):
a=0
b=0
c=0
d=0
f=0
if grade == 'A':
a += 1
print ('A: ' +str(a))
elif grade == 'B':
b += 1
print ('B: ' +str(b))
elif grade == 'C':
c += 1
print ('C: ' +str(c))
elif grade == 'D':
d +=1
print ('D: ' +str(d))
elif grade == 'F':
f +=1
print ('F: ' +str(f))
tavge = read_test_scores()
print ('Grade Distribution')
print_comment(grade)
print ("Test Average is: " + str(tavge))`
Upvotes: 0
Views: 1596
Reputation: 31
Here's what I think you were going for. I had to make several changes that I can clarify if needed... Mostly you needed to store the scores and grades as an array of values instead of individual ones. When you need to, for instance, tally up the number of A's, B's, etc. you just loop through the array and add up the occurrences.
def read_test_scores():
print("Enter number of students: ")
num = int(input())
scores = []
print("Enter the next grade: ")
for score in range(num):
score = int(input())
while score < 0 or score > 100:
print('Score should be between 0 and 100, try again:')
score = int(input())
scores.append(score)
sum_scores = sum(scores)
tavge = sum_scores / num
return tavge, scores
def get_letter_grade(n):
if 90 <= n <= 100:
grade = 'A'
elif 80 <= n <= 89:
grade = 'B'
elif 70 <= n <= 79:
grade = 'C'
elif 60 <= n <= 69:
grade = 'D'
elif n<60:
grade = 'F'
return grade
def print_comment(grades):
a=0
b=0
c=0
d=0
f=0
for grade in grades:
if grade == 'A':
a += 1
elif grade == 'B':
b += 1
elif grade == 'C':
c += 1
elif grade == 'D':
d +=1
elif grade == 'F':
f +=1
print ('A: ' +str(a))
print ('B: ' +str(b))
print ('C: ' +str(c))
print ('D: ' +str(d))
print ('F: ' +str(f))
tavge, scores = read_test_scores()
grades = []
for score in scores:
grades.append(get_letter_grade(score))
print ('Grade Distribution')
print_comment(grades)
print ("Test Average is: " + str(tavge))
Upvotes: 2
Reputation: 348
This is my idea
Get number of students
Create total_score, or any Something you need to record
for in Students
Get score then add in total_score
total_score = 0
for i in range(student_number) :
score = int(input())
total_score += score
# get_letter_grade
average = total_score / student_number
print ("Test Average is" + str())
Upvotes: 0