-2

I'm trying to figure out how to get try/except to work within a function.

Here is what I have now and it produces a traceback error when I enter anything not numeric (e.g. "forty").

def computepay(hours=float(input('Enter hours worked: ')), rate=float(input('Enter hourly rate: '))):
    try:
        if hours <= 40:
         print('pay:', hours * rate)
        else:
            ot_rate = rate * 1.5
            ot_hours = hours - 40
            print('pay: $', (ot_hours * ot_rate) + (40 * rate))
    except NameError:
        print('Error, please enter numeric input')


computepay()
khelwood
  • 55,782
  • 14
  • 81
  • 108
  • Default argument values is very much not the right place to read user input. – khelwood May 06 '22 at 18:31
  • 1
    Don't place statements inside function defenition. It's a very bad practice. – TDG May 06 '22 at 18:31
  • You are trying to convert user input to a `float` up in your function arguments which are very much outside of your try/except block. – JNevill May 06 '22 at 18:31
  • If I am not wrong, that should be TypeError instead of a NameError. – Free Space May 06 '22 at 18:33
  • You should be catching a `ValueError` not a `NameError` if you expect the `float` conversion to fail – Cory Kramer May 06 '22 at 18:34
  • 1
    In addition to those `input` calls being bad practice, you'll run afoul of [Mutable Default Argument](https://stackoverflow.com/questions/1132941/least-astonishment-and-the-mutable-default-argument), in that they'll only be called once, when the function is defined, not once per function invocation. – Silvio Mayolo May 06 '22 at 18:36

3 Answers3

2

Your function should not deal with user input. It should deal only with computing and display resilts.

def computepay(hours: float, rate: float) -> None:
  if hours <= 40:
   print('pay:', hours * rate)
  else:
      ot_rate = rate * 1.5
      ot_hours = hours - 40
      print('pay: $', (ot_hours * ot_rate) + (40 * rate))


try:
  hours = float(input('Enter hours worked:'))
  rate = float(input('Enter hourly rate: '))
  computepay(hours,rate)
except ValueError :
  print('input must be numeric')
balderman
  • 22,927
  • 7
  • 34
  • 52
0

You must handle the input errors separately. That way the code can't proceed if the inputs are invalid.

def computepay(hours, rate):
  try:
    if hours <= 40:
     print('pay:', hours * rate)
    else:
        ot_rate = rate * 1.5
        ot_hours = hours - 40
        print('pay: $', (ot_hours * ot_rate) + (40 * rate))


try:
    var hours = float(input('Enter hours worked: '))
    var rate = float(input('Enter hourly rate: '))
    computepay(hours, rate)
except ValueError:
    print('Error, please enter numeric input')

Hope this helps.

Qedrix
  • 453
  • 1
  • 8
  • 15
-1

You can handle the user's input within the function; when you try to convert as an argument there is no mechanism to support the possibility that the input is not a floating value.

def computepay(hours=(input('Enter hours worked: ')), rate=(input('Enter hourly rate: '))):
    try: 
        hours = float(hours)  
        rate = float(rate)            
        if hours <= 40:
            print('pay:', hours * rate)
        else:
            ot_rate = rate * 1.5
            ot_hours = hours - 40
            print('pay: $', (ot_hours * ot_rate) + (40 * rate))
    except ValueError:
        print('Error, please enter numeric input')


computepay()
Shawn Ramirez
  • 796
  • 1
  • 5
  • 10