Python:SyntaxError'return'外部函数错误

时间:2014-02-15 23:25:02

标签: python python-3.x

我的代码出现SyntaxError 'return outside function'错误。我很感激任何有关这方面的帮助,因为这令我感到沮丧。

def temp( T, from_unit, to_unit ) : # Function for temperature
        """ Convert between Fahrenheit, Celsius, or Kelvin. Where from_unit and to_unit are temperature units, either 'F' (or 'f') 
        for Fahrenheit, or 'C' (or 'c') for Celsius, or 'K'(or 'k') for Kelvin; and T is a temperature number (of float) for the
        unit from_unit """

    if from_unit == to_unit:  
        return T

3 个答案:

答案 0 :(得分:2)

小心缩进。

我认为您需要将“if”语句缩进到与“”字符串相同的级别。

答案 1 :(得分:2)

你的docstring比if条件更进一步缩进。 docstrings是Python对象,因此是代码的一部分。确保代码均匀缩进。

答案 2 :(得分:1)

SyntaxError:'return'外部函数 它只是缩进错误移动你的return语句,错误将丢失