returning None instead of value: how to fix?

S

sam

i am starting to experiment with recursion, and decided to write a
fairly trivial little program which took a float as input, then called
a function to halve it recursively until it was less than 1:

____________________________________________________________________
import recursive_halve_module

raw_value = raw_input('please enter the number you would like to halve:
')
value = float(raw_value)

final_value = recursive_halve_module.recursive_halve(value)

print final_value

raw_input('hit enter to close: ')
_____________________________________________________________________

def recursive_halve(value):

if value < 1:
print value
return value
else:
value = value/2
print value
if value < 1:
return value
else:
recursive_halve(value)
______________________________________________________________________

it works fine apart from printing 'None' instead of 'final_value' at
the end. however, if you enter a value that is already less than 1, it
prints 'final_value' (i.e. that very same number) just fine.

now, it looks to me like only 'value' can be returned: either right at
the beginning (for values less than 1) or when you eventually get down
to below 1 after x function calls. but clearly that's not the case. i
understand that 'None' is returned by default by functions in Python.
my question is: how am i slipping into that default despite seemingly
(to me at least) avoiding it through explicitly returning something
else?

thanks in advance,

sam
 
N

Neil Cerutti

i am starting to experiment with recursion, and decided to
write a fairly trivial little program which took a float as
input, then called a function to halve it recursively until it
was less than 1:

____________________________________________________________________
import recursive_halve_module

raw_value = raw_input('please enter the number you would like to halve:
')
value = float(raw_value)

final_value = recursive_halve_module.recursive_halve(value)

print final_value

raw_input('hit enter to close: ')
_____________________________________________________________________

def recursive_halve(value):

if value < 1:
print value
return value
else:
value = value/2
print value
if value < 1:
return value
else:
recursive_halve(value)

Your immediate bug is that you're missing a return statement:

return recursive_halve(value)

But your recursive routine is overcomplicated with too many base
cases, and you haven't written it functionally. There's no need
to modify value directly in a functional procedure.

Here's another version.

def recursive_halve(value):
if value < 1.0:
return value
else:
return recursive_halve(value / 2.0)

Now imagine the call-stack when called with value 45.0 (using the
substitution model):

recursive_halve(45.0) -->
return recursive_halve(22.5) -->
return recursive_halve(11.25) -->
return recursive_halve(5.625) -->
return recursive_halve(2.8125) -->
return recursive_halve(1.40625) -->
return recursive_halve(0.703125) -->
return 0.703125
 
G

georgeryoung

sam said:
i am starting to experiment with recursion, and decided to write a
fairly trivial little program which took a float as input, then called
a function to halve it recursively until it was less than 1:
____________________________________________________________________
import recursive_halve_module

raw_value = raw_input('please enter the number you would like to halve: ')
value = float(raw_value)
final_value = recursive_halve_module.recursive_halve(value)
print final_value
raw_input('hit enter to close:')
def recursive_halve(value):
if value < 1:
print value
return value
else:
value = value/2
print value
if value < 1:
return value
else:
recursive_halve(value)
^^^^^^^
This needs to be:
return recursive_halve(value)
You were executing the function, but not returning the result.

-- George
 
C

Carl Banks

sam said:
def recursive_halve(value):

if value < 1:
print value
return value
else:
value = value/2
print value
if value < 1:
return value
else:
recursive_halve(value)

Missing a return on the last line is likely your immediate problem.

You have more subtle problems, though. First, you have needlessly
reduplicated value<1 test--the first thing recursive_halve does is to
check whether value<1, so there's no need to check whether value<1
before calling it.

Second, you have redundant branching logic. Look at your first else
statement. It does nothing; the else doesn't affect whether the stuff
inside it gets executed or not; it's redundant. It's better style to
not to have such redundancy (either by omitting the else, or having a
single return point).

The following should do exactly what you want.

def recursive_halve(value):
if value < 1:
print value
return value
value = value/2
print value
return recursive_halve(value)


Carl Banks
 
S

sam

Missing a return on the last line is likely your immediate problem.

thanks to everyone for pointing this out. obviously, i had not
understood what was actually involved in a recursive call. i have
corrected it as suggested and it works fine now.
You have more subtle problems, though. First, you have needlessly
reduplicated value<1 test--the first thing recursive_halve does is to
check whether value<1, so there's no need to check whether value<1
before calling it.

yes, i could see this was nasty. i figured i'd go back and try and
streamline it afterwards. i wrote an insertion sort yesterday and i had
to cheat by putting two items into a new list to begin with, and only
then enter the nested loops to do the rest of it. i seem to be fighting
my way into a process to get a foothold, and only then trying to finish
it smoothly/elegantly. hopefully this will ease up with time and
practice.
Second, you have redundant branching logic. Look at your first else
statement. It does nothing; the else doesn't affect whether the stuff
inside it gets executed or not; it's redundant. It's better style to
not to have such redundancy (either by omitting the else, or having a
single return point).

if i understand this correctly, you mean that the function will
terminate if it returns 'value' at the beginning, so the else: is
implicit in the fact that the interpreter has even got that far. i take
your point about trying to abbreviate what is, in effect, verbiage.

thanks again for all the answers. it boggles the mind that one can have
access to this almost immediate help at no charge.

sam
 
N

Neil Cerutti

thanks to everyone for pointing this out. obviously, i had not
understood what was actually involved in a recursive call. i
have corrected it as suggested and it works fine now.

thanks again for all the answers. it boggles the mind that one
can have access to this almost immediate help at no charge.

But there is a charge. You're charged with asking your question
well, and you're charged with making some obvious effort to solve
the problem on your own. Those who take the trouble, as you did,
usually get excellent free help.

It's not out of the kindness of our hearts that we help. Heck, I
don't know what it is. Probably I just like reading my own drivel
on the internet and occassionally helping others is a good
excuse.
 
B

Bruno Desthuilliers

sam a écrit :
i am starting to experiment with recursion, and decided to write a
fairly trivial little program which took a float as input, then called
a function to halve it recursively until it was less than 1:

And forgot to return the result from the recursive call, I guess ?-)
 
B

Bruno Desthuilliers

Neil Cerutti a écrit :
(snip)
It's not out of the kindness of our hearts that we help. Heck, I
don't know what it is. Probably I just like reading my own drivel
on the internet and occassionally helping others is a good
excuse.

Lol !-)

+1 OTQOTW
 
G

George Sakkis

Neil said:
It's not out of the kindness of our hearts that we help. Heck, I
don't know what it is. Probably I just like reading my own drivel
on the internet and occassionally helping others is a good
excuse.

Weird, isn't it ? Good to know that it's not just me that thinks this
way.

Best,
George
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,770
Messages
2,569,584
Members
45,077
Latest member
SangMoor21

Latest Threads

Top