Python:SyntaxError'return'外部函数

2024-05-14 08:13:41 发布

您现在位置:Python中文网/ 问答频道 /正文

我的代码有一个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

Tags: orandto代码fromforreturnunit
3条回答

docstring的缩进比if条件的缩进更远。docstring是Python对象,因此也是代码的一部分。确保代码缩进一致。

SyntaxError:“return”外部函数 它只是缩进错误移位返回语句,错误将丢失

小心压痕。

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

相关问题 更多 >

    热门问题