lundi 1 mai 2017

Try except not catching error

Sorry in advance, beginner... I've googled try/except help for the last day now and don't know what I'm doing incorrectly.

These are 2 beginner exercises. Simple problems but I'm also trying to add an error message if the input is non-numeric. I've tried try/except and now trying if/else but both don't get activated (e.g. if the user enters "ten percent", the is an error output versus my error message)

The first one calculates grade based on a percentage input. The second is supposed to calculate pay.

grade=eval(input("Enter Score:"))
try:
if(grade<0 or grade>1):
    print("Bad Score")
elif(grade>=0.9):
    print("A")
elif(grade<=0.9 and grade>=0.8):
    print("B")
elif(grade<=0.8 and grade>=0.7):
    print("C")
elif(grade<=0.7 and grade>=0.6):
    print("D")
else:
    print("F")
except:
    print("Bad score")

Hours=eval(input('Please enter hours worked: '))
Rate=eval(input('Please enter pay per hour: '))
if(Hours<=40 and Hours>=0):
    Pay=Hours*Rate
elif(Hours>40):
    Pay=((Hours-40)*(1.5*Rate))+(40*Rate)
    print('Your pay should be $',Pay)
else:
    print('Error.  Please enter a Numeric Value')

Thank you in advance...

Aucun commentaire:

Enregistrer un commentaire