[Tutor] Understanding error in recursive function

Mark Lawrence breamoreboy at yahoo.co.uk
Fri Apr 8 19:12:30 EDT 2016


On 08/04/2016 21:48, Tom Maher wrote:
> Hi,
>
> As a test I am trying to write a function that returns the sum of values
> attached to one key in a dictionary. I am wondering why the code that I
> wrote is returning:
> "maximum recursion depth exceeded"
> Here is the code:
>
> animals = { 'a': ['aardvark'], 'b': ['baboon'], 'c': ['coati']}
> x = animals['a']
> def howMany(aDict):
>        count = 0
>        for value in howMany(aDict):
>             count += 1
>        return count
>
> howMany(x)
>
> Just wondering why I would be getting that error.
>
> Thanks

howMany calls howMany which calls howMany...

-- 
My fellow Pythonistas, ask not what our language can do for you, ask
what you can do for our language.

Mark Lawrence



More information about the Tutor mailing list