How do I fix this error: TypeError: unsupported operand type(s) for *: 'float' and 'function' in python
Question:
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
Answers:
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)
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.
Additional notes.
In response to the comment about having "total_hrs_per_week" being a function that returns a value instead of being a variable, following is a revised code snippet that is equivalent to the above code snippet but calls a function named "total_hrs_per_wk" and uses the returned value.
# Have total weekly hours as a function instead of a variable
def total_hrs_per_wk(pay, reg_rate, total_hrs):
return float(total_hrs - pay / reg_rate)
#calculate overtime pay
def overtime_pay(weekly_pay, regular_rate, overtime_hrs ):
total_hr = float(weekly_pay / regular_rate + overtime_hrs)
overtime_p = weekly_pay + ((regular_rate*1.5)*overtime_hrs)
print('Total pay with overtime for ', total_hr, '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, total_hrs_per_wk(weekly_pay, regular_rate, (40 + overtime_hrs))) # Last parameter will be the result of a function call
Testing resulted in the same values displaying on the terminal. As noted earlier, this would just be one of many ways to get the same result.
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
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)
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.
Additional notes.
In response to the comment about having "total_hrs_per_week" being a function that returns a value instead of being a variable, following is a revised code snippet that is equivalent to the above code snippet but calls a function named "total_hrs_per_wk" and uses the returned value.
# Have total weekly hours as a function instead of a variable
def total_hrs_per_wk(pay, reg_rate, total_hrs):
return float(total_hrs - pay / reg_rate)
#calculate overtime pay
def overtime_pay(weekly_pay, regular_rate, overtime_hrs ):
total_hr = float(weekly_pay / regular_rate + overtime_hrs)
overtime_p = weekly_pay + ((regular_rate*1.5)*overtime_hrs)
print('Total pay with overtime for ', total_hr, '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, total_hrs_per_wk(weekly_pay, regular_rate, (40 + overtime_hrs))) # Last parameter will be the result of a function call
Testing resulted in the same values displaying on the terminal. As noted earlier, this would just be one of many ways to get the same result.