2

I've created a tail recursive function to solve an optimization problem:

def optimize(current_price = 0.1, last_profit = 0.0):
    current_profit = profit(current_price)
    if (last_profit > current_profit) and (current_profit > 0.0):
        return {'best_price': current_price - 0.1, 'best_profit': last_profit}
        # print({'best_price': current_price - 0.1, 'best_profit': last_profit})
    else:
        optimize(current_price + 0.1, current_profit)

def best_price():
    optimized = optimize() # optimize() should return a dict, 
                           # allowing optimized['best_price'] 
                           # and optimized['best_profit'] to be called
    print("Pricing the tickets at ${0} will produce the greatest profit, ${1}.".format(optimized['best_price'], optimized['best_profit']))

The function operates correctly with the exception that it fails to return anything. I do not mean to say that the first if statement is never called (in fact, when I uncomment the print line, it will print the correct result), but that the return statement fails to return a dictionary.

This results in a TypeError when I attempt to call optimized['best_price'], as 'NoneType' object is not subscriptable.

I've been working on this error for a while now, and can't seem to either make it work myself or find anything regarding it online. At this point, it's just a matter of me wanting to know the solution. Any ideas? Thanks!

1
  • Why don't you have return in each alternative of the if? Commented Sep 7, 2011 at 21:21

1 Answer 1

5

Even a tail-recursive function needs a return in Python:

def optimize(current_price = 0.1, last_profit = 0.0):
    current_profit = profit(current_price)
    if (last_profit > current_profit) and (current_profit > 0.0):
        return {'best_price': current_price - 0.1, 'best_profit': last_profit}
    else: # Add return below here
        return optimize(current_price + 0.1, current_profit)
Sign up to request clarification or add additional context in comments.

7 Comments

Perfect! Thank you! Can you explain why this is the case? (For instance, why would the print function work while the return statement wouldn't?)
"why would the print function work while the return statement wouldn't"? What? Print-- as in show output-- has nothing to do with returning a useful value from a recursive function. What are you asking? You should -- perhaps -- search for questions on recursion. If none of those questions are adequate, you may have to open a new question.
I apologize if you misunderstood my question. Let me rephrase it: Why does the exclusion of the second return affect my ability to return the dictionary, but not my ability to print the same? I would understand if the exclusion negated my ability to recurse at all, but the function was clearly recursing without it, which allowed the print to output the correct result. I'm sure that there is something I'm missing, but recursion is a broad subject, and I'm simply looking for guidance in further research.
@Zachary Allaun: "the function was clearly recursing. the print to output the correct result." Right. And. You had no return so no value was returned. It doesn't seem mysterious. No return means no value gets returned. It doesn't mean recursion stops. It only means a value isn't returned. Recursion is not a "broad subject". It's just a technique.
Adam calls Bob. Bob calls Celine. Celine calls Dana. Dana says "Hi!". Celine says "Bye!". Bob says nothing. What does Adam hear?
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.