You could simply use
return
which does exactly the same as
return None
Your function will also return None if execution reaches the end of the function body without hitting a return statement. Returning nothing is the same as returning None in Python.
You could simply use
return
which does exactly the same as
return None
Your function will also return None if execution reaches the end of the function body without hitting a return statement. Returning nothing is the same as returning None in Python.
I would suggest:
def foo(element):
do something
if not check: return
do more (because check was succesful)
do much much more...
How to get out function in Python?
How do I exit the program without exit()?
Can I stop a function from another function?
Exiting a program without exit(), quit(), raise SystemExit, etc...
Guys I am building a Billing Program(Project). I am a beginner and am coding this project on python using functions. I am using different functions for Menu and Veg non veg beverages etc...So first the Menu is printed and then the User Selects what he wants . But when he clicks Veg or Beverage Menu(for example) I display all the items on the respective Menu including option to exit. But I cannot exit or terminate the function using return. Can anyone suggest how to get out of the function and again return the Main-menu using return or break*
The exit() function is not allowed in this task, and I've tried everything in the little Python knowledge I have to exit the function where I want it to.
def ask_if_continue():
cont = raw_input("Do you wish to continue? Type 'y' for yes, 'n' for no:")
if cont == "y":
print ("You have chosen to continue.")
else:
print ("You have chosen not to continue.")
return contSo, as shown, the program needs to exit after the user inputs 'n' and the 'You have chosen not to continue' afterwards. What it does now is go on to the next lines of code regardless of whether I type 'y' or 'n', obviously.
Perhaps enclosing each message in separate functions would help? I think that's part of the task but I'm not 100% sure.
Can anyone help this Python newbie out? Help is much appreciated :)
Hi guys I'm pretty new in python and I'm doing some exercises. In one of that needs to stop the whole program when one thing happen.
I would like to know if it is possibile to stop a function when something happen in another function.
My function 'B' stop when the user insert in input a specific value, the function 'A' that has to stop at the same time, doesn't.
Helloings,
on a basic computer course I ran into a problem where I can't use any built-in exit functions or libraries, but I have to somehow terminate the program mid-function. Simply breaking out of that function won't likely help as it would just move onto the next function with invalid values, crashing the whole thing. Any tips?
Why quit() is not stopping the execution of code after print(x) ?
x=4
if x>0:
print(x)
quit()
print("niat")
else:
print("ekse")
output is:
4
niat
You have two options (at least).
Using a return statement:
def do_something(parameter):
if parameter > 100:
# display error message if necessary
return # 'exit' function and return to caller
# rest of the code
You can also return soemthing passing the something value back to the caller. This can be used to provide a status code for instance (e.g. 0: success, 1: error).
Or a better approach is to raise an exception:
def do_something(parameter):
if parameter > 100:
raise ValueError('Parameter should...')
# rest of the code
try:
do_something(101)
except ValueError, e:
# display error message if necessary e.g. print str(e)
See exceptions in the Python manual.
There are built-in exception classes (like ValueError above). You can also define your own as follows:
class ParameterError(Exception):
pass
You can also add additional code to your custom exception classes to process parameters, display custom error messages, etc...
The built-in exceptions are listed here.
Define a custom exception, and raise it.
class MyError(Exception):
pass
...
if parameter > 100:
# quit the function and any function(s) that may have called it
raise MyError('Your parameter should not be greater than 100!')
(although actually, now I think about it, you could just use a built-in exception: ValueError would seem appropriate).