繁体   English   中英

如何修复此错误:TypeError: unsupported operand type(s) for *: 'float' and 'function' in python

[英]How do I fix this error: TypeError: unsupported operand type(s) for *: 'float' and 'function' in python

如何解决此 TypeError

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

这是我的代码:

#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

您的意思是这里的最后一个变量是overtime_p吗?

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

您的代码片段缺少一些重要信息,例如“total_hrs_per_wk”以及调用此 function 的支持代码。 所以,我使用了一些艺术许可来填补缺失的部分。 以下是利用您的加班 function 以及一些修订的代码片段。

#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)

以下是从代码片段中删除的一些内容。

  • 由于没有“total_hrs_per_wk”的定义,因此添加了一个计算,通过将工人的正常工作时间与加班时间相加来得出一周的总小时数。
  • 由于变量“total_hrs_per_wk”包含一个数值而不是一个字符串,因此 print 语句被更改为将值打印为数字而不是您最初指定的字符串。
  • 打印语句中的最后一个变量“overtime_pay”被更正为“overtime_p”。 “overtime_pay”是您的 function 的名称,打印该值只会打印出 function 定义详细信息,而不是您所追求的加班费。

试试看。

补充说明。

为了回应关于“total_hrs_per_week”是一个返回值而不是变量的 function 的评论,下面是一个修改后的代码片段,它等效于上述代码片段,但调用名为“total_hrs_per_wk”的 function 并使用返回的值.

# 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

测试导致终端上显示相同的值。 如前所述,这只是获得相同结果的众多方法之一。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM