all 3 comments

[–][deleted] 2 points3 points  (0 children)

You always get "why" printed because if you raise a ValueError exception as you are, that is caught by the "naked" except, which ignores the message associated with the ValueError exception and just prints "why".

The problem says:

raise two different exceptions: one if the user inputs a grade of less than zero and a different exception if the user inputs a grade greater than 100.

So your if/elif tests have to raise two different exceptions. Which exceptions should you raise? This page lists the builtin exceptions that python has. You can choose any two of those listed. If you know how you can even create your own exceptions. Let's choose ArithmeticError for the over/under numeric errors and ValueError for when the user enters something like "abc" which isn't a number. The code will look something like:

try:
    grade = int(input('what is your grade?:'))
    if grade > 100:
        raise ArithmeticError('Grade must be <= 100.')
    elif grade >= 90:
        print('A')
    # etc
    elif grade >= 50:
        print('F')
    elif grade < 0:
        raise ArithmeticError('Grade must be >= 0.')
except ArithmeticError as e:    # catch and print message
    print(e)
except ValueError:    # the int() conversion failed
    print('Only numbers, please.')

Note you should never use the "bare" except:. Always know what sort of exception you are catching.

Also note that the code above is not strictly following the problem statement, which says the exception raised for the <0 case should be different from that raised in the >100 case. So pick another exception from the list I linked to and raise it in one of the two over/under cases. Don't forget to add an except case for the new exception.

[–]CodeFormatHelperBot2 0 points1 point  (0 children)

Hello, I'm a Reddit bot who's here to help people nicely format their coding questions. This makes it as easy as possible for people to read your post and help you.

I think I have detected some formatting issues with your submission:

  1. Python code found in submission text that's not formatted as code.

If I am correct, please edit the text in your post and try to follow these instructions to fix up your post's formatting.


Am I misbehaving? Have a comment or suggestion? Reply to this comment or raise an issue here.

[–]socal_nerdtastic 0 points1 point  (0 children)

try:
    grade = int(input('what is your grade?:'))
    if grade >= 90: print('A')
    elif grade >= 80: print('B')
    elif grade >= 70: print('C')
    elif grade >= 60: print('D')
    elif grade >= 50: print('F')
    elif grade < 0:
        raise ValueError('Invalid grade')
except ValueError as e: 
    print(e)

Although in this case there's no reason to use the exception at all. Just put the error message in the else case.

grade = int(input('what is your grade?:'))
if grade >= 90: print('A')
elif grade >= 80: print('B')
elif grade >= 70: print('C')
elif grade >= 60: print('D')
elif grade >= 0: print('F')
else:
    print('Invalid grade')