-1

I have this so far - but if I test with (-a, b), python gives me a recursion error. Please help, not sure why this isn't working. All other tests work here.

def mult(a, b):
""" mult returns the product of two inputs
    inputs: n and m are integers
    output: result of multiplying n and m
""" 
    if b < 0:
       return -mult(a,-b)
    elif b == 0:
       return 0
    elif b == 1:
       return a
    else:
       return a + mult(a,b-1)

Thanks out there.

wwii
  • 23,232
  • 7
  • 37
  • 77
J. Doe
  • 1
  • 1
  • 1
    Please show the full traceback of the error. Also, the indentation of your shown code is wrong, so you should get a syntax error. Finally, the check for `b == 1` is extraneous and not needed: `b` will become zero in the next recursion step. – Rory Daulton Sep 11 '16 at 22:18
  • 1
    This code works for me, after correcting the indentation. Please show the values for which the function fails. – David Harris Sep 11 '16 at 23:02

1 Answers1

1

Python has a limit to the number of recursions. You may just be hitting into it. See the following answer:

What is the maximum recursion depth in Python, and how to increase it?

Community
  • 1
  • 1
user2539336
  • 180
  • 6