Your mistake has to do with the definition of the problem. The man does not stop taking matches as soon as one of the boxes is empty (as in your code). He stops taking matches after he picks a box and finds it empty. This means that when Box A is empty, he doesn't immediately stop -- he continues until he picks Box A again (or until he empties Box B as well).
EDIT: In fact, it's trivial to fix your code -- you don't even have to rewrite your function! Just add the following:
def bar (n=3):
return foo(n+1) - 1
This just exploits the fact that picking the empty matchbox in the $n$-match problem is equivalent to emptying the box in the $(n+1)$-match problem.