Python TypeError: temperature() missing 1 required positional argument: 'float'

Python TypeError: temperature() missing 1 required positional argument: 'float'

刚开始学习Python。我尝试创建一个新函数,但终端一直返回相同类型的错误。

TypeError: temperature() missing 1 required positional argument: '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")

你能帮我弄清楚这是怎么回事吗?

这不应该是您要找的吗?从温度函数的参数列表中删除浮点数?使用摄氏度和浮点数作为参数,您的温度函数期望您向它传递两个参数。你只需要传递一摄氏度。

此外,您需要在打印前存储温度方法的return。请注意我是如何使用 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")

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