-4

I'm 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", there 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')

Edited for formatting... code was correct in the original post but had to indent to create code grey box, creating incorrect indents.

Thank you once again!

WW C
  • 11
  • 2
  • 1
    [Fix the indentation](http://stackoverflow.com/posts/43722313/edit). Your code won't even run as it is now. – khelwood May 01 '17 at 16:11
  • 1
    I guess you want something like http://stackoverflow.com/q/23294658/3001761, but also [don't use bare `except`](http://blog.codekills.net/2011/09/29/the-evils-of--except--/). – jonrsharpe May 01 '17 at 16:11
  • 2
    @jonrsharpe Nor `eval`.. – DeepSpace May 01 '17 at 16:14
  • Thank you everyone! The link was what I was looking for. – WW C May 01 '17 at 16:22
  • For future questioners, just in case, this while true catch was a good idea. Thank you again for the check. while True: try: grade=float(input("Enter Score:")) except ValueError: print("Bad score") continue else: break – WW C May 01 '17 at 16:48

1 Answers1

-1

Bad inedentation! Try and expect should go on there own line. Like so:

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')
PyDever
  • 100
  • 2
  • 10