Why does my function return None?
up vote
32
down vote
favorite
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.
python function recursion return
add a comment |
up vote
32
down vote
favorite
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.
python function recursion return
7
You need to doreturn Dat_Function()
when calling it recursively.
– Gustav Larsson
Jul 22 '13 at 0:31
2
Just a tip: The idiomatic way of thatmy_var != "a" and my_var != "b"
condition would bemy_var not in ('a', 'b')
– gonz
May 18 '16 at 1:04
add a comment |
up vote
32
down vote
favorite
up vote
32
down vote
favorite
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.
python function recursion return
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.
python function recursion return
python function recursion return
edited Apr 11 at 0:01
Aran-Fey
20.3k53266
20.3k53266
asked Jul 22 '13 at 0:29
Cate
177127
177127
7
You need to doreturn Dat_Function()
when calling it recursively.
– Gustav Larsson
Jul 22 '13 at 0:31
2
Just a tip: The idiomatic way of thatmy_var != "a" and my_var != "b"
condition would bemy_var not in ('a', 'b')
– gonz
May 18 '16 at 1:04
add a comment |
7
You need to doreturn Dat_Function()
when calling it recursively.
– Gustav Larsson
Jul 22 '13 at 0:31
2
Just a tip: The idiomatic way of thatmy_var != "a" and my_var != "b"
condition would bemy_var not in ('a', 'b')
– gonz
May 18 '16 at 1:04
7
7
You need to do
return Dat_Function()
when calling it recursively.– Gustav Larsson
Jul 22 '13 at 0:31
You need to do
return Dat_Function()
when calling it recursively.– Gustav Larsson
Jul 22 '13 at 0:31
2
2
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
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
add a comment |
4 Answers
4
active
oldest
votes
up vote
48
down vote
accepted
It is returning None
because when you recursively call it:
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:
>>> def f(x):
... pass
>>> print(f(20))
None
So, instead of just calling Dat Function()
in your if
statement, you need to return
it.
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 inmain()
?
– Cate
Jul 22 '13 at 0:49
1
You lost me with thatmain()
bit... You can fail as many times as you want to, the one that "succeeds" will returnmy_var
, which will get passed down (return
ed) through all of the recursive calls all the way down to the original caller. Which, yes, ismain()
.
– roippi
Jul 22 '13 at 0:54
I was thinking that when youreturn Dat_Function()
you're really just callingDat_Function()
again inmain()
.Dat_Function()
now returns a function andmain()
has go call it.
– Cate
Jul 22 '13 at 0:58
|
show 1 more comment
up vote
6
down vote
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
add a comment |
up vote
3
down vote
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()
add a comment |
up vote
-1
down vote
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:
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.
add a comment |
4 Answers
4
active
oldest
votes
4 Answers
4
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
48
down vote
accepted
It is returning None
because when you recursively call it:
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:
>>> def f(x):
... pass
>>> print(f(20))
None
So, instead of just calling Dat Function()
in your if
statement, you need to return
it.
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 inmain()
?
– Cate
Jul 22 '13 at 0:49
1
You lost me with thatmain()
bit... You can fail as many times as you want to, the one that "succeeds" will returnmy_var
, which will get passed down (return
ed) through all of the recursive calls all the way down to the original caller. Which, yes, ismain()
.
– roippi
Jul 22 '13 at 0:54
I was thinking that when youreturn Dat_Function()
you're really just callingDat_Function()
again inmain()
.Dat_Function()
now returns a function andmain()
has go call it.
– Cate
Jul 22 '13 at 0:58
|
show 1 more comment
up vote
48
down vote
accepted
It is returning None
because when you recursively call it:
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:
>>> def f(x):
... pass
>>> print(f(20))
None
So, instead of just calling Dat Function()
in your if
statement, you need to return
it.
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 inmain()
?
– Cate
Jul 22 '13 at 0:49
1
You lost me with thatmain()
bit... You can fail as many times as you want to, the one that "succeeds" will returnmy_var
, which will get passed down (return
ed) through all of the recursive calls all the way down to the original caller. Which, yes, ismain()
.
– roippi
Jul 22 '13 at 0:54
I was thinking that when youreturn Dat_Function()
you're really just callingDat_Function()
again inmain()
.Dat_Function()
now returns a function andmain()
has go call it.
– Cate
Jul 22 '13 at 0:58
|
show 1 more comment
up vote
48
down vote
accepted
up vote
48
down vote
accepted
It is returning None
because when you recursively call it:
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:
>>> def f(x):
... pass
>>> print(f(20))
None
So, instead of just calling Dat Function()
in your if
statement, you need to return
it.
It is returning None
because when you recursively call it:
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:
>>> def f(x):
... pass
>>> print(f(20))
None
So, instead of just calling Dat Function()
in your if
statement, you need to return
it.
edited Jul 22 '13 at 0:38
answered Jul 22 '13 at 0:31
roippi
19.6k33053
19.6k33053
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 inmain()
?
– Cate
Jul 22 '13 at 0:49
1
You lost me with thatmain()
bit... You can fail as many times as you want to, the one that "succeeds" will returnmy_var
, which will get passed down (return
ed) through all of the recursive calls all the way down to the original caller. Which, yes, ismain()
.
– roippi
Jul 22 '13 at 0:54
I was thinking that when youreturn Dat_Function()
you're really just callingDat_Function()
again inmain()
.Dat_Function()
now returns a function andmain()
has go call it.
– Cate
Jul 22 '13 at 0:58
|
show 1 more comment
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 inmain()
?
– Cate
Jul 22 '13 at 0:49
1
You lost me with thatmain()
bit... You can fail as many times as you want to, the one that "succeeds" will returnmy_var
, which will get passed down (return
ed) through all of the recursive calls all the way down to the original caller. Which, yes, ismain()
.
– roippi
Jul 22 '13 at 0:54
I was thinking that when youreturn Dat_Function()
you're really just callingDat_Function()
again inmain()
.Dat_Function()
now returns a function andmain()
has go call it.
– Cate
Jul 22 '13 at 0:58
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
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
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
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
1
1
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 (return
ed) 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
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 (return
ed) 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
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
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
|
show 1 more comment
up vote
6
down vote
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
add a comment |
up vote
6
down vote
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
add a comment |
up vote
6
down vote
up vote
6
down vote
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.
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.
answered Jul 22 '13 at 0:32
Simon
611
611
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
add a comment |
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
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
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
add a comment |
up vote
3
down vote
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()
add a comment |
up vote
3
down vote
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()
add a comment |
up vote
3
down vote
up vote
3
down vote
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()
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()
edited May 18 '16 at 0:59
gonz
3,45323147
3,45323147
answered May 17 '16 at 23:34
user6348168
391
391
add a comment |
add a comment |
up vote
-1
down vote
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:
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.
add a comment |
up vote
-1
down vote
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:
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.
add a comment |
up vote
-1
down vote
up vote
-1
down vote
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:
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.
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:
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.
answered Feb 14 at 16:37
asylturatbek dooranov
1
1
add a comment |
add a comment |
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f17778372%2fwhy-does-my-function-return-none%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
7
You need to do
return Dat_Function()
when calling it recursively.– Gustav Larsson
Jul 22 '13 at 0:31
2
Just a tip: The idiomatic way of that
my_var != "a" and my_var != "b"
condition would bemy_var not in ('a', 'b')
– gonz
May 18 '16 at 1:04