python syntaxerror: ‘return’ outside function

In this Python tutorial, we will discuss how to fix an error, syntaxerror return outside function python, and can’t assign to function call in python The error return outside function python comes while working with function in python.

Python syntaxerror: ‘return’ outside function

In python, this error can come when the indentation or return function does not match.

Example:

def add(x, y):
  sum = x + y
return(sum)
print(" Total is: ", add(20, 50))

After writing the above code (syntaxerror return outside function python), Ones you will print then the error will appear as a “ SyntaxError return outside function python ”. Here, line no 3 is not indented or align due to which it throws an error ‘return’ outside the function.

You can refer to the below screenshot python syntaxerror: ‘return’ outside function

Syntaxerror return outside function python
python syntaxerror: ‘return’ outside function

To solve this SyntaxError: return outside function python we need to check the code whether the indentation is correct or not and also the return statement should be inside the function so that this error can be resolved.

Example:

def add(x, y):
  sum = x + y
  return(sum)
print(" Total is: ", add(20, 50))

After writing the above code (syntaxerror return outside function python), Once you will print then the output will appear as a “ Total is: 70 ”. Here, line no. 3 is resolved by giving the correct indentation of the return statement which should be inside the function so, in this way we can solve this syntax error.

You can refer to the below screenshot:

Syntaxerror return outside function python
return outside function python

SyntaxError can’t assign to function call in python

In python, syntaxerror: can’t assign to function call error occurs if you try to assign a value to a function call. This means that we are trying to assign a value to a function.

Example:

chocolate = [
     { "name": "Perk", "sold":934 },
     { "name": "Kit Kat", "sold": 1200},
     { "name": "Dairy Milk Silk", "sold": 1208},
     { "name": "Kit Kat", "sold": 984}
]
def sold_1000_times(chocolate):
    top_sellers = []
    for c in chocolate:
        if c["sold"] > 1000:
            top_sellers.append(c)
    return top_sellers
sold_1000_times(chocolate) = top_sellers
print(top_sellers)

After writing the above code (syntaxerror: can’t assign to function call in python), Ones you will print “top_sellers” then the error will appear as a “ SyntaxError: cannot assign to function call ”. Here, we get the error because we’re trying to assign a value to a function call.

You can refer to the below screenshot cannot assign to function call in python

SyntaxError can't assign to function call in python
SyntaxError can’t assign to function call in python

To solve this syntaxerror: can’t assign to function call we have to assign a function call to a variable. We have to declare the variable first followed by an equals sign, followed by the value that should be assigned to that variable. So, we reversed the order of our variable declaration.

Example:

chocolate = [
     { "name": "Perk", "sold":934 },
     { "name": "Kit Kat", "sold": 1200},
     { "name": "Dairy Milk Silk", "sold": 1208},
     { "name": "Kit Kat", "sold": 984}
]
def sold_1000_times(chocolate):
    top_sellers = []
    for c in chocolate:
        if c["sold"] > 1000:
            top_sellers.append(c)
    return top_sellers
top_sellers
 = sold_1000_times(chocolate)
print(top_sellers)

After writing the above code (cannot assign to function call in python), Ones you will print then the output will appear as “[{ “name”: “Kit Kat”, “sold”: 1200}, {“name”: “Dairy Milk Silk”, “sold”: 1208}] ”. Here, the error is resolved by giving the variable name first followed by the value that should be assigned to that variable.

You can refer to the below screenshot cannot assign to function call in python is resolved

SyntaxError can't assign to function call in python
SyntaxError can’t assign to function call in python

You may like the following Python tutorials:

This is how to solve python SyntaxError: return outside function error and SyntaxError can’t assign to function call in python. This post will be helpful for the below error messages:

  • syntaxerror return outside function
  • python syntaxerror: ‘return’ outside function
  • return outside of function python
  • return’ outside function python
  • python error return outside function
  • python ‘return’ outside function
  • syntaxerror return not in function