1
print('Hello, welcome to your grade calculator.')
GradeCount=0
totalGrades=0.0
moreStudent='y'

while moreStudent=='y' or moreStudent=='Y':
    grade=float(input('Enter a grade or a -1 to end: '))
    while grade !=-1:
        if grade>100 or grade<0:
            print('Invalid input. Please enter a value between 1 and 100.')
        elif grade>=90 and grade<=100:
            print('You got an A. Thats awesome.')
        elif grade>= 80 and grade<=89:
            print('You got a B. Good job.')
        elif grade>= 70 and grade<=79:
            print('You got a C. Thats fine I guess.')
        elif grade>=60 and grade<=69:
            print ('You got a D. Not very good.')
        elif grade<60:
            print ('You got an F. You fail.')
        totalGrades=totalGrades + grade
        GradeCount=GradeCount + 1
        grade=float(input('Enter the next grade or -1 to end: '))
    moreStudent=input('Are you a new student and ready to enter your grades? 
y or n: ')

print ('Class grade average:' , format(totalGrades/GradeCount, '.2f'))
print ('Number of grades entered:',GradeCount)

Basically, at the top where it validates the input, how can I prevent any invalid input from being included in the average below? Also, how can I add a running count of grades entered, and a running total of the grades?

coco
  • 79
  • 3

1 Answers1

0

You can just put a continue at the end of your 'Invalid input' condition. Because it's in a while loop, you also have to reset grade by copy/pasting your input call (grade=float(input('Enter the next grade or -1 to end: '))).

While you're at it, you can simplify your conditions a little bit by doing lower_limit <= grade < upper_limit. With the changes, it looks like this (again, the critical part is the continue and the line before it):

print('Hello, welcome to your grade calculator.')
GradeCount = 0
totalGrades = 0.0
moreStudent = 'y'

while moreStudent == 'y' or moreStudent == 'Y':
    grade = float(input('Enter a grade or a -1 to end: '))
    while grade != -1:
        if grade > 100 or grade < 0:
            print('Invalid input. Please enter a value between 1 and 100.')
            grade = float(input('Enter the next grade or -1 to end: '))
            continue
        elif 90 <= grade <= 100:
            print('You got an A. Thats awesome.')
        elif 80 <= grade < 90:
            print('You got a B. Good job.')
        elif 70 <= grade < 80:
            print('You got a C. Thats fine I guess.')
        elif 60 <= grade < 70:
            print ('You got a D. Not very good.')
        elif grade < 60:
            print ('You got an F. You fail.')
        totalGrades = totalGrades + grade
        GradeCount = GradeCount + 1
        grade = float(input('Enter the next grade or -1 to end: '))
    moreStudent = input('Are you a new student and ready to enter your grades? y or n: ')

print ('Class grade average:', format(totalGrades / GradeCount, '.2f'))
print ('Number of grades entered:', GradeCount)

You could also solve it by nesting all your elifs inside an else counterpart to if grade > 100 or grad < 0.

EL_DON
  • 1,416
  • 1
  • 19
  • 34