x = True
def myFunc():
if x:
return "yes"
else:
return "false"
print("hello world")
myFunc()
This code gave me error as: "This code is unreachable"
Please can you explain why this is ?
With return
keywords, the def
stops when the code runs into a return
line. It assigns the specified value to the function, and then it will stop. In your case, you have a bool
type on an if/else
condition. So x
must be True
or False
, and both cases have a return
statement. If you want to do anything after that, you should call it outside the original function.
So, with that being said:
x = True
def myFunc():
if x:
return "yes"
else:
return "false"
myFunc()
print("hello world")
Hope this helps.