如何使公式在函数中执行?

我收到TypeError:*:'float'和'function'不支持的操作数类型。

我是编程的新手,所以我的经验和经验很少。我正在尝试在函数中执行windchill公式,但似乎收到错误,任何人都可以提供有关我要去哪里的提示或建议吗?

函数get_wind

  1. 此功能将从用户输入风速
  2. 并将风速返回到调用函数
  3. 功能输入:无
  4. 函数输出:返回用户输入的风速
def WindChill(temp,wind):
    value_1=temp
    value_2=wind
    value_3=(35.74+0.625*temp-35.75*wind**0.16+0.4275*temp*wind**0.16)
    temp=get_temp
    wind=get_wind
    return value_3

WindChill()

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

yoyocom_33 回答:如何使公式在函数中执行?

首先,Python对缩进非常挑剔。它基本上定义了函数中的内容和未包含的内容。从上面的代码看来,您正在函数内部调用该函数。不知道这是否是复制/粘贴错误,只是不确定。

第二,按照书面规定,您必须将参数传递给函数。您将函数定义为

def WindChill(temp,wind):

如果您希望函数调用get_temp和get_wind,则不需要任何参数。

已更新:我可以自由地将您的代码复制到编辑器中,并稍作更改,它可以正常运行。我假设get_temp和get_wind从传感器或某些东西读取了一些值。因此,我在示例中对它们进行了硬编码。

def get_temp():
    result = input("Enter the temp: ")
    return result

def get_wind():
    result = input("Enter the wind speed: ")
    return result

def WindChill():
    temp = get_temp()
    wind = get_wind()
    value_3 = (35.74 + 0.625 * temp - 35.75 * wind ** 0.16 + 0.4275 * temp * wind ** 0.16)
    return value_3


def main():
    print("%.2f" % WindChill())

if __name__== "__main__":
    main()

结果是:

Enter the temp: 70
Enter the wind speed: 12
70.82

我将其限制为两位小数,因为它很难看。没必要只是我就是我。

,
  1. 没有理由加上括号。
  2. 缩进必须非常精确。您的return语句后的函数调用无法缩进,否则Python会认为它是该函数的一部分。
  3. 该函数调用需要两个参数,因为您的函数具有两个参数。

尝试以下操作:

def WindChill(temp,wind):
    return 35.74 + 0.625 * temp - 35.75 * wind ** 0.16 + 0.4275 * temp * wind ** 0.16


print(WindChill(24,4))
  

18.98200054

,

如果以这种方式格式化,我看不到此代码有问题:

def WindChill(temp,wind): 
    value_1=temp 
    value_2=wind 
    value_3=(35.74+0.625*temp-35.75*wind**0.16+0.4275*temp*wind**0.16) 
    #temp=get_temp 
    #wind=get_wind 
    return value_3 
WindChill(94.3,33.2) 
本文链接:https://www.f2er.com/3130316.html

大家都在问