Python TypeError:temperature()缺少1个必需的位置参数:'float'

问题描述 投票:0回答:2

我刚刚开始学习Python。我试图创建一个新函数,但是终端不断返回同样的错误。

TypeError:temperature()缺少1个必需的位置参数:'float'

下面是我的代码:

def temperature(celsius, float) -> float:
        fahrenheit = (celsius*(9/5)+32)
        return fahrenheit
celsius = float(input("Input the temperature in Celsius that you want to have in Fahrenheit: "))
temperature(celsius)
print("The temperature in Fahrenheit is: ", temperature, "°F")

您能帮我弄清楚发生了什么吗?

python spyder
2个回答
1
投票

这不是您要寻找的。从温度功能的参数列表中删除浮子?将摄氏度和浮点数作为参数,您的温度函数希望您将其传递给两个参数。您只需将其传递一摄氏度即可。

此外,在打印之前,您需要存储温度方法的返回值。请注意,我是如何使用temp变量将最后一行修改为行的。您将温度函数调用的输出存储在temp中。然后在最后一行打印temp。

def temperature(celsius) -> float:
    fahrenheit = (celsius*(9/5)+32)
    return fahrenheit
celsius = float(input("Input the temperature in Celsius that you want to have in Fahrenheit: "))
temp = temperature(celsius)
print("The temperature in Fahrenheit is: ", temp, "°F")

0
投票

为什么您需要功能定义为def temperature(celsius, float)而不是def temperature(celsius)?它将与后一个定义完全相同,并且不会返回任何错误

© www.soinside.com 2019 - 2024. All rights reserved.