Home > OS >  How do I fix this error: TypeError: unsupported operand type(s) for *: 'float' and 'f
How do I fix this error: TypeError: unsupported operand type(s) for *: 'float' and 'f

Time:09-10

How do I resolve this TypeError

TypeError: unsupported operand type(s) for *: 'float' and 'function'

Here is my code:

#calculate overtime pay
def overtime_pay(weekly_pay, regular_rate, overtime_hrs ):
  overtime_p = weekly_pay   ((regular_rate*1.5)*overtime_hrs)
  print('Overtime pay for '   total_hrs_per_wk   'hours worked'   'is: \t', overtime_pay)
  return overtime_p

CodePudding user response:

Did you mean for the last variable here to be overtime_p?

print('Overtime pay for '   total_hrs_per_wk   'hours worked'   'is: \t', overtime_pay)

CodePudding user response:

Your code snippet was missing some vital bits of information such as "total_hrs_per_wk" along with supporting code to call this function. So, I used a bit of artistic license to fill in the missing bits. Following is a snippet of code that utilizes your overtime function along with some revisons.

#calculate overtime pay
def overtime_pay(weekly_pay, regular_rate, overtime_hrs ):
    total_hrs_per_wk = float(weekly_pay / regular_rate   overtime_hrs) # This variable was missing - added it
    overtime_p = weekly_pay   ((regular_rate*1.5)*overtime_hrs)
    print('Total pay with overtime for ', total_hrs_per_wk, 'hours worked is: \t', overtime_p)  # Made the last variable "overtime_p" instead of the function name
    return overtime_p

# Created the following code to execute the function with floating point variables
    
weekly_pay = float(input("Enter weekly pay: "))
regular_rate = float(input("Enter worker rate: "))
overtime_hrs = float(input("Enter overtime hours: "))

overtime_pay(weekly_pay, regular_rate, overtime_hrs)

Here are some of the things to take away from the code snippet.

  • Since there was no definition for "total_hrs_per_wk" a calculation was added to derive the total hours for the week by adding in the normal hours the worker would have to the overtime hours.
  • Since the variable "total_hrs_per_wk" contains a numeric value and is not a string, the print statement is changed to print the value out as a number instead of a string as you had originally specified.
  • The last variable in the print statement, "overtime_pay", is corrected to be "overtime_p". "overtime_pay" is the name of your function and printing that value will just print out function definition details and not the overtime pay you are after.

Give that a try.

  • Related