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
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 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
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
You may like the following Python tutorials:
- Remove character from string Python
- Create an empty array in Python
- Invalid syntax in python
- syntaxerror invalid character in identifier python3
- How to handle indexerror: string index out of range in Python
- Unexpected EOF while parsing Python
- Python built-in functions with examples
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
Python is one of the most popular languages in the United States of America. I have been working with Python for a long time and I have expertise in working with various libraries on Tkinter, Pandas, NumPy, Turtle, Django, Matplotlib, Tensorflow, Scipy, Scikit-Learn, etc… I have experience in working with various clients in countries like United States, Canada, United Kingdom, Australia, New Zealand, etc. Check out my profile.