returning None instead of value: how to fix?

sam python.sam at googlemail.com
Fri Sep 22 16:59:58 EDT 2006


> 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




More information about the Python-list mailing list