2

I need to creat a grade calculator that puts toughater the results of 5 test and avareges them, and gives out the result of the 5 grades, I made the final grade result program, but I can't figure out how to make the loop that runs only five times and sum each inputed results.

grade = 0
total = 0
for grade in range(0,5):

 if grade >= 0 and grade <= 100:
  grade = grade + int(input('What was your score: '))

 elif grade >= 101: 

  grade += int(input('It should be a number from 0 to 100, what was your 
score: '))

if 93<=grade <= 100:
   print 'A'
elif 90 <= grade < 93:
  print 'A-'
elif 87 <= grade < 90:
  print 'B+'
elif 83 <= grade < 87:
  print 'B'
elif 80 <= grade < 83:
  print 'B-'
elif 77 <= grade < 80:
  print 'C+'
elif 73 <= grade < 77:
  print 'C'
elif 70 <= grade < 73:
  print 'C-'
elif 67 <= grade < 60:
  print 'D+'
elif 63 <= grade < 67:
  print 'D'
elif 60 <= grade < 63:
  print 'D-'
elif grade < 60:
  print 'F'

1 Answers1

2

You are not properly collecting the grade from the user's input as you are also adding the increment value (0 - 5) to the grade which would lead to incorrect results.

For the loop, what you can do is to maintain a count of the number of grades entered correctly and run it until it reaches the limit which in your case is 5.

Here is a possible fix to your code:

total = 0
gradeCount = 0

while gradeCount < 5:
    grade = int(input('What was your score: '))

    if grade < 0 or grade > 100:
        print('It should be a number from 0 to 100')
    else:
        gradeCount += 1
        total += grade

        if 93 <= grade <= 100:
           print('A')
        elif 90 <= grade < 93:
            print('A-')
        elif 87 <= grade < 90:
            print('B+')
        elif 83 <= grade < 87:
            print('B')
        elif 80 <= grade < 83:
            print('B-')
        elif 77 <= grade < 80:
            print('C+')
        elif 73 <= grade < 77:
            print('C')
        elif 70 <= grade < 73:
            print('C-')
        elif 67 <= grade < 60:
            print('D+')
        elif 63 <= grade < 67:
            print('D')
        elif 60 <= grade < 63:
            print('D-')
        elif grade < 60:
            print('F')

average = total / gradeCount

print('Average: ' + str(average))
Omari Celestine
  • 1,405
  • 1
  • 11
  • 21