1

I am getting a SyntaxError 'return outside function' error on my code. I would appreciate any help in regards to this as this is frustrating me to no end.

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
1
  • 2
    To those voting to close this question, I'm not so sure this is a simple typographical error. It's not really obvious the docstring should be indented at the same level as the code, since most new users would think of the docstring as a glorified comment, which has no indentation restrictions. For evidence, take a look at the answer of @BenWhaley, who called the docstring a comment (which illustrates how easily they can be confused.) Commented Feb 16, 2014 at 1:21

3 Answers 3

2

Your docstring is indented further than the if conditional. docstrings are Python objects and as such are part of the code. Make sure the code is indented uniformly.

Sign up to request clarification or add additional context in comments.

Comments

2

Be careful with indentation.

I think you need to have the "if" statement indented at the same level as the """ string.

2 Comments

You're absolutely correct, I manage to fix the error using that. I have started to pick up python again and indentations are a problem for me. Thanks for your help, the both of you.
I've been using python for ages and indentation issues still drive me up the wall.... get used to it!
1

SyntaxError: 'return' outside function Its just indentation error shift your return statement and error will be lost

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.