0

I am trying to average grades using a while loop. Here is the program requirements:

Let the user enter grades to average. The user may type any number of grades; when he or she types “quit” or “Quit” print the average, rounded to two decimal places.

Please keep advice relatively simple. First time programming. Only am familiar with for loops and while loops and if else statements right now.

I was given this skeleton code:

def main():
    '''Average grades entered by the user'''
    #Set the total to 0
    #Set the grade count to 0
    #Get a grade
    #While the grade is not "quit" or "Quit"
    #    Add the grade to total
    #    Add one to the grade count
    #    Get a grade
    #Average the grades
    #Round the average to two decimal places
    #Print the average

main()  #Start execution

Here is my code thus far:

def main():
    '''Average grades entered by the user'''
    total = 0#Set the total to 0
    grade_count = 0#Set the grade count to 0
    grade = int(input('Enter grade :'))#Get a grade
    while grade != 'quit' or grade != 'Quit':#While the grade is not "quit" or "Quit"
        total += grade#    Add the grade to total
        grade_count += 1#    Add one to the grade count
        grade = int(input('Enter grade :'))#    Get a grade
    average = total / grade_count#Average the grades
    round(average, 2)#Round the average to two decimal places
    print(average)#Print the average

main()  #Start execution

And this is the error I am getting:

Enter grade :70
Enter grade :80
Enter grade :90
Enter grade :quit
Traceback (most recent call last):
  File "\\trace\Class\CSC-130\004\Rall992\Asn2\Asn2-1.py", line 21, in        <module>
    main()  #Start execution
  File "\\trace\Class\CSC-130\004\Rall992\Asn2\Asn2-1.py", line 16, in main
    grade = int(input('Enter grade :'))#    Get a grade
ValueError: invalid literal for int() with base 10: 'quit'
Bhargav Rao
  • 50,140
  • 28
  • 121
  • 140
B. Allan
  • 53
  • 5
  • 3
    Well, what number do you expect `quit` to be? You need to check the input before you convert it to an int. – Morgan Thrapp Oct 05 '16 at 20:19
  • Yes, I understand the reason for the error but do not know how to fix it. – B. Allan Oct 05 '16 at 20:20
  • 3
    Do not cast `grade` to an `int()` until the next lines where you add to `total` – OneCricketeer Oct 05 '16 at 20:21
  • Would you show me this? @cricket_007 – B. Allan Oct 05 '16 at 20:22
  • 1
    You need to check for 'quit' before casting the input to an int. Change the assignment to input('Enter grade :'), then first line in while loop set grade = int(grade) – K Richardson Oct 05 '16 at 20:22
  • 1
    For example, `grade != 'quit'` will never be true if `grade` could be converted to a number. Just use `grade = input("Enter a grade")`, then compare `if grade.lower() != 'quit'` – OneCricketeer Oct 05 '16 at 20:23
  • 1
    More relevant duplicate.. https://stackoverflow.com/questions/29290556/grade-average-calculator?rq=1 – OneCricketeer Oct 05 '16 at 20:24
  • 1
    You could put the `grade = int(input('Enter grade :'))` statement inside a `try/except ValueError:` and handle the exception accordingly. – martineau Oct 05 '16 at 20:33
  • 1
    Change your first line in your while loop to `total += int(grade)`. Then in your second user input, use `grade = raw_input('Enter grade :')` because otherwise Python will interpret you trying to quit the program (i.e. `exit()`) – Mangohero1 Oct 05 '16 at 21:12

0 Answers0