当我在另一个函数中调用一个函数时,返回值不会被打印出来,因此我需要使用print函数

问题描述

我创建了这两个函数computepay()和calculateOverTime。我在computepay中调用了calculateOverTime,进行了数学运算并将其返回。我希望calculateOverTime的结果数学运算能通过print(computepay(floatHours,floatRate))打印出来。我在做什么错了?

def calculateOverTime(floatHours,floatRate):
    overtimeHours = floatHours - 40
    regularPay = (floatHours-overtimeHours)*floatRate
    overtimeRate = floatRate * 1.5
    overtimePay = overtimeHours*overtimeRate
    overtimePayment = overtimePay+regularPay
    return overtimePayment

def computepay(floatHours,floatRate):
if floatRate<=40:
    if floatHours>40:
        calculateOverTime(floatHours,floatRate)
    else:
            regularPay = floatHours*floatRate
            return regularPay
    else:
        print("I can't process this shit")

try:
    floatHours = input("Enter the hours:")
    floatHours = float(floatHours)
    floatRate = input("Enter the rate:")
    floatRate = float(floatRate)

except:
    print("Wrong Inputs")
    input("Try again")

print(computepay(floatHours,floatRate))

input("Close please")

解决方法

您在调用calculateOverTime方法时错过了返回。

return calculateOverTime(floatHours,floatRate)
,
def computepay(floatHours,floatRate):
    if floatRate<=40:
        if floatHours > 40:
            amount=calculateOverTime(floatHours,floatRate)
            return amount

        else:
            regularPay = floatHours * floatRate
            return regularPay
    else:
        print("I can't process this shit")

您缺少返回我接受了可变数量然后返回的calculateOverTime(floatHours,floatRate)的信息。您也可以直接使用return CalculationOverTime(floatHours,floatRate)

,

似乎您没有将calculateOverTime()的结果分配给任何东西。

所以不要这样做

if floatHours>40: calculateOverTime(floatHours,floatRate)

您应该写

if floatHours>40: return calculateOverTime(floatHours,floatRate)

,

我检查了您的代码并使其正常工作。

您缺少在calculateOverTime函数中返回compute_pay函数的功能,因此,不仅要调用该函数,还需要在开头添加一个返回值:

return calculateOverTime(floatHours,floatRate)

相关问答

依赖报错 idea导入项目后依赖报错,解决方案:https://blog....
错误1:代码生成器依赖和mybatis依赖冲突 启动项目时报错如下...
错误1:gradle项目控制台输出为乱码 # 解决方案:https://bl...
错误还原:在查询的过程中,传入的workType为0时,该条件不起...
报错如下,gcc版本太低 ^ server.c:5346:31: 错误:‘struct...