How can I correct this python syntax error? SyntaxError: 'return' outside function (user_script_6e47e023d7da445ea3f8cca0bfde24d9.py, line 9)'.

Anonymous
2023-09-26T04:47:07.97+00:00

I have tried every possible combination of spaces and tabs in this very simple copy/paste from the learning module. I continue to get either an error on line 9 for 'return' outside function, or, an 'unexpected indent' on one of the previous lines. Can someone help me identify what needs to change?

Appreciate the help as I run through this learning module.

import pandas as pd

def azureml_main(dataframe1 = None, dataframe2 = None):

    scored_results = dataframe1[['Scored Labels']]
    scored_results.rename(columns={'Scored Labels':'predicted_price'},
                     inplace=True)
return scored_results
Azure Machine Learning
Azure Machine Learning
An Azure machine learning service for building and deploying models.
3,333 questions
Azure AI services
Azure AI services
A group of Azure services, SDKs, and APIs designed to make apps more intelligent, engaging, and discoverable.
3,601 questions
0 comments No comments
{count} votes

Accepted answer
  1. Ramr-msft 17,826 Reputation points
    2023-09-26T07:34:31.9933333+00:00

    @Anonymous Thanks for the question, you are encountering a syntax error due to the 'return' statement being outside of a function.

    In Python, the 'return' statement is used to exit a function and return a value to the caller. However, it can only be used inside a function definition. In your code, the 'return' statement is outside of any function, which is causing the syntax error.

    import pandas as pd
    
    def scored_results(dataframe1):
        results = dataframe1[['Scored Labels']]
        results.rename(columns={'Scored Labels':'predicted_price'}, inplace=True)
        return results
    
    1 person found this answer helpful.

0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.