代码之家  ›  专栏  ›  技术社区  ›  subhashis

尝试执行简单程序时出现类型错误?

  •  -1
  • subhashis  · 技术社区  · 5 年前

    嗨,我收到以下代码的TypeError

    def fahrenheit(self,T_in_celsius):
    """ returns the temperature in degrees Fahrenheit"""
    return (T_in_celsius * 9 / 5) + 32
    
    for t in (22.6, 25.8, 27.3, 29.8):
        print(t,":",fahrenheit(t)) 
    

    例外情况:

    print(t,":",fahrenheit(t))  
    TypeError: fahrenheit() missing 1 required positional argument: 'T_in_celsius'
    

    值在print()中变为t: 22.6 : 25.8 : 27.3 : 29.8 :

    1 回复  |  直到 5 年前
        1
  •  1
  •   Nightara    5 年前

    我认为你在这里混淆了(类)方法和(静态)函数。 类方法(类中未加注释的Aka函数 @staticmethod )通常有 self (或者,从技术上讲,任何名称,但这是最常见的选择)作为第一个参数,它包含对调用此方法的对象的引用。

    在你的例子中,我没有看到任何类或对象,所以我假设你想创建一个(静态)函数。请忽略 自己 ,因为它只对方法是必需的,对函数不是必需的。