return outside function python что делать
Python SyntaxError: ‘return’ outside function Solution
A return statement sends a value from a function to a main program. If you specify a return statement outside of a function, you’ll encounter the “SyntaxError: ‘return’ outside function” error.
- Career Karma matches you with top tech bootcamps Get exclusive scholarships and prep courses
- Career Karma matches you with top tech bootcamps Get exclusive scholarships and prep courses
In this guide, we explore what the “‘return’ outside function” error means and why it is raised. We’ll walk through an example of this error so you can figure out how to solve it in your program.
SyntaxError: ‘return’ outside function
Return statements can only be included in a function. This is because return statements send values from a function to a main program. Without a function from which to send values, a return statement would have no clear purpose.
Return statements come at the end of a block of code in a function. Consider the following example:
Our return statement is the final line of code in our function. A return statement may be used in an if statement to specify multiple potential values that a function could return.
An Example Scenario
We’re going to write a program that calculates whether a student has passed or failed a computing test. To start, let’s define a function that checks whether a student has passed or failed. The pass-fail boundary for the test is 50 marks.
Our function can return two values: True or False. If a student’s grade is over 50 (above the pass-fail boundary), the value True is returned to our program. Otherwise, the value False is returned. Our program prints the value “Checked” no matter what the outcome of our if statement is so that we can be sure a grade has been checked.
Now that we have written this function, we can call it in our main program. First, we need to ask the user for the name of the student whose grade the program should check, and for the grade that student earned. We can do this using an input() statement:
81% of participants stated they felt more confident about their tech job prospects after attending a bootcamp. Get matched to a bootcamp today.
Find Your Bootcamp Match
The average bootcamp grad spent less than six months in career transition, from starting a bootcamp to finding their first job.
Start your career switch today
The value of “grade” is converted to an integer so we can compare it with the value 50 in our function. Let’s call our function to check if a student has passed their computing test:
We call the check_if_passed() function to determine whether a student has passed their test. If the student passed their test, a message is printed to the console telling us they passed; otherwise, we are informed the student failed their test.
Let’s run our code to see if it works:
An error is returned.
The Solution
We have specified a return statement outside of a function. Let’s go back to our check_if_passed() function. If we look at the last line of code, we can see that our last return statement is not properly indented.
The statement that returns False appears after our function, rather than at the end of our function. We can fix this error by intending our return statement to the correct level:
The return statement is now part of our function. It will return the value False if a student’s grade is not over 50. Let’s run our program again:
Return outside function Python: How to Resolve This Error
In Python, don’t use the return statement to get out of the loop. If you do that, then you will face the SyntaxError. Instead, use the break or quit() statement to break the loop or terminate the program.
Return outside function Python
When you are working in a loop, you mistakenly use the return statement to break the loop instead of using the break or quit() function. If you are using return, you will get the SyntaxError: return outside function in Python. This syntax error suggests that you are not using the return statement within the function because you are using it in the loop, and that loop is not enclosed by a function.
For example, see the below code.
The above while loop should not have a return statement because it does not break the loop.
It does not make any sense to stop a while loop by using the return statement.
If you run the above code, then you will get the following output.
We get the SyntaxError: ‘return‘ outside function.
To resolve the return outside function SyntaxError in Python, create a function and write the loop inside that function, and then use the return function. This is the correct way to use the return.
Output
And we resolved the error and get the expected output.
Using break keyword
To break the loop in Python, use the break keyword. The break statement ends the loop. If the break statement is inside the nested loop (loop inside another loop), the break will terminate the innermost loop.
Syntax
Example
Let’s take a for loop and range() function to iterate in-range elements and break the loop if certain conditions are met.
Output
In the first iteration, the conditions are not met, so it prints 5, and in the second iteration, when i = 2, it breaks the loop because i = 2 condition satisfied. It executes the break keyword, which ultimately stops the execution.
Using quit() function in Python
To exit the Python program, use the quit() function. As soon as the system finds the quit() function, it terminates the program’s execution immediately.
Syntax
Example
Take a range() function and for loop to iterate through elements, and if certain conditions are met, it will quit the program using the quit() function.
Output
You can see that it immediately quits the program when i = 2.
It raises the SystemExit exception behind the scenes. If you print it, it will give a message.
That is it for “return outside function” SyntaxError in Python Tutorial.
syntax error ‘return’ outside function in python
I am trying to count the word fizz using python. However it is giving me an error.
i checked with indentation but still it does not work. Where i am doing wrong?
5 Answers 5
Your indentation seems to be incorrect, and you should not have the first return count (why would you return count as soon as you define it??).
Please try the following code: remove return count right after count = 0
There are also a few indentation changes.
the problem is the identation in your line return
You do not need the first ‘return’ statement in your code. It works as follows, with indentation and spacing fixed:
Well i am new to python world. What i learned is return statement should be some thing like this.
Not the answer you’re looking for? Browse other questions tagged python or ask your own question.
Linked
Related
Hot Network Questions
Subscribe to RSS
To subscribe to this RSS feed, copy and paste this URL into your RSS reader.
site design / logo © 2021 Stack Exchange Inc; user contributions licensed under cc by-sa. rev 2021.11.12.40742
By clicking “Accept all cookies”, you agree Stack Exchange can store cookies on your device and disclose information in accordance with our Cookie Policy.
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:
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:
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:
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:
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:
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:
Entrepreneur, Founder, Author, Blogger, Trainer, and more. Check out my profile.
SyntaxError: ‘return’ outside function [duplicate]
I have a list, somewhat similar to the one below.
lines = [‘This is line 1’,
‘This is another line’,
‘This is the third line. Line 03.’]
When I run the return statement to process for the len of the line,
generates the following error:
File «», line 2
return(len(line))
^
SyntaxError: ‘return’ outside function
I can however print the lengths of the lines,
How exactly is the return statement outside function in this instance?
Edit: Here is what it looks like in my Notebook.
2 Answers 2
The return function is used to pass a value back to where a certain function is called. The way I see it, you’re essentially trying to overwrite the return value. You should use it in a function that does something and returns the value back. Your loop and return statement don’t appear to be in a function.
In example below I take each item in the list, pass it into the check_len function, which obviously checks length of the item and returns it. Then, the length of each item in the list is printed.
Your indention might be inconsistent. Use four spaces per indention level as recommended by PEP-8. Secondly, it should be inside a function. Thirdly, your return statement won’t return length of all the items as you want.