help
help copied to clipboard
SyntaxError: 'return' outside function
I'm trying to use an if statement in my notebook but keep receiving the message?
SyntaxError: 'return' outside function
Even the simplest if statement receives that message, for example:
if 2==2:
return 1
My indentation seems right, I don't know what's causing it.
return
statements must be inside functions, e.g.
def foo():
if True:
return 1
You can't have a return statement outside a function because without a function there isn't anything to return from.
How can i return from the place where rhe number is called if it is wrong choice like this
[] Enter Your Choice: 1 [] Wrong Choice ! [*] Enter Your Choice:
Something like this
You can use if
statements to control when certain things happen. For input validation, you can use a while
loop to wait for a valid input:
while True:
choice = input('your choice: ')
if choice == 1:
# valid choice, stop asking!
break
else:
print("invalid, try again!")