Why does my function return None?

Clash Royale CLAN TAG#URR8PPP
Why does my function return None?
This may be an easy question to answer, but I can't get this simple program to work and it's driving me crazy. I have this piece of code:
def Dat_Function():
my_var = raw_input("Type "a" or "b": ")
if my_var != "a" and my_var != "b":
print "You didn't type "a" or "b". Try again."
print " "
Dat_Function()
else:
print my_var, "-from Dat_Function"
return my_var
def main():
print Dat_Function(), "-From main()"
main()
Now, if I input just "a" or "b",everything is fine. The output is:
Type "a" or "b": a
a -from Dat_Function
a -From main()
But, if I type something else and then "a" or "b", I get this:
Type "a" or "b": purple
You didn't type "a" or "b". Try again.
Type "a" or "b": a
a -from Dat_Function
None -From main()
I don't know why Dat_Function() is returning None, since it should only return my_var. The print statement shows that my_var is the correct value, but the function doesn't return that value for some reason.
Dat_Function()
None
my_var
my_var
return Dat_Function()
Just a tip: The idiomatic way of that
my_var != "a" and my_var != "b" condition would be my_var not in ('a', 'b')– gonz
May 18 '16 at 1:04
my_var != "a" and my_var != "b"
my_var not in ('a', 'b')
4 Answers
4
It is returning None because when you recursively call it:
None
if my_var != "a" and my_var != "b":
print "You didn't type "a" or "b". Try again."
print " "
Dat_Function()
..you don't return the value.
So while the recursion does happen, the return value gets discarded, and then you fall off the end of the function. Falling off the end of the function means that python implicitly returns None, just like this:
None
>>> def f(x):
... pass
>>> print(f(20))
None
So, instead of just calling Dat Function() in your if statement, you need to return it.
Dat Function()
if
return
Shouldn't it run through the if statement again if it is called recursively? I don't understand why it wouldn't return a value.
– Cate
Jul 22 '13 at 0:35
Nope. See my edit. The recursion happens, and then you discard what the recursion returns.
– roippi
Jul 22 '13 at 0:38
So if you call a function from inside that same function the return value gets discarded, but you return the same function in that function you really just call it in
main()?– Cate
Jul 22 '13 at 0:49
main()
You lost me with that
main() bit... You can fail as many times as you want to, the one that "succeeds" will return my_var, which will get passed down (returned) through all of the recursive calls all the way down to the original caller. Which, yes, is main().– roippi
Jul 22 '13 at 0:54
main()
my_var
return
main()
I was thinking that when you
return Dat_Function() you're really just calling Dat_Function() again in main(). Dat_Function() now returns a function and main() has go call it.– Cate
Jul 22 '13 at 0:58
return Dat_Function()
Dat_Function()
main()
Dat_Function()
main()
To return a value other than None, you need to use a return statement.
In your case, the if block only executes a return when executing one branch. Either move the return outside of the if/else block, or have returns in both options.
I've tried moving it out of the block, but to no avail. Instead of returning the correct value, it returns the first incorrect value. Also, I don't want a return statement for the if part of the if/else statement because I want the function to only return a correct value.
– Cate
Jul 22 '13 at 0:39
def Dat_Function():
my_var = raw_input("Type "a" or "b": ")
if my_var != "a" and my_var != "b":
print "You didn't type "a" or "b". Try again."
print " "
return Dat_Function()
else:
print my_var, "-from Dat_Function"
return my_var
def main():
print Dat_Function(), "-From main()"
main()
I think that you should use while loops.
if my_var != "a" and my_var != "b":
print "You didn't type "a" or "b". Try again."
print " "
return Dat_Function()
Consider that you type something different than "a" and "b", of course, it will call Dat_Function but then it is skipping the next part. Which is:
Dat_Function
else:
print my_var, "-from Dat_Function"
return my_var
And will go directly into:
def main():
print Dat_Function(), "-From main()"
So, if you use while loop as:
while my_var!="a" and my_var!="b":
print ('you didn't type a or b')
return Dat_Function()
This way I think that you can handle it.
By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.
You need to do
return Dat_Function()when calling it recursively.– Gustav Larsson
Jul 22 '13 at 0:31