Re: [Tutor] Understanding error in recursive function

2016-04-16 Thread Alan Gauld
On 10/04/16 19:36, Peter Otten wrote: > Alan Gauld wrote: > >> or a slightly sneaky but faster version: >> >> def howMany(aList): >> return aList.index(aList[-1]) + 1 > > Sorry, but this isn't "slightly sneaky", this is outright wrong. > I fails for both empty lists and lists with duplicate e

Re: [Tutor] Understanding error in recursive function

2016-04-10 Thread Peter Otten
Alan Gauld wrote: > or a slightly sneaky but faster version: > > def howMany(aList): > return aList.index(aList[-1]) + 1 Sorry, but this isn't "slightly sneaky", this is outright wrong. I fails for both empty lists and lists with duplicate entries.

Re: [Tutor] Understanding error in recursive function

2016-04-09 Thread Oliver Bestwalter
Hi Tom, You can see what happens for yourself in the Pythontutor: http://goo.gl/YVkh03 - step through the code by clicking on "Forward" and see what happens. You are calling the howMany function from inside the function itself, which is called recursion. This can be practical, but in your case it

Re: [Tutor] Understanding error in recursive function

2016-04-09 Thread khalil zakaria Zemmoura
Hi, I think your function runs forever because the only thing it done is calling it self when entering the for loop when you call your function in the for loop it jump to the definition (def howMany(aDict)) then initialise count and bond the value 0 to it then enter a for loop then call the funct

Re: [Tutor] Understanding error in recursive function

2016-04-09 Thread Mark Lawrence via Tutor
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':

Re: [Tutor] Understanding error in recursive function

2016-04-08 Thread Alan Gauld
On 09/04/16 00:34, Martin A. Brown wrote: > You start by creating a dictionary that uses an initial letter as > the key and a list to hold the names of the animals. Your function > could probably be as simple as: > > def howMany(aList): > return len(aList) > > Here is an inefficien

Re: [Tutor] Understanding error in recursive function

2016-04-08 Thread Alan Gauld
On 08/04/16 21:48, Tom Maher wrote: > 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 " Python has a limit to how many times you can recursi

Re: [Tutor] Understanding error in recursive function

2016-04-08 Thread Martin A. Brown
Greetings Tom, >As a test I am trying to write a function that returns the sum of >values Given the code you pasted, I think you mean 'count' of values, right? I'll assume that below, because it is a tricky thing to sum some strings. >attached to one key in a dictionary. I am wondering why

Re: [Tutor] Understanding error in recursive function

2016-04-08 Thread Danny Yoo
On Fri, Apr 8, 2016 at 1:48 PM, 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. Why does the program try to use recursion for this problem? ___ Tutor maillist -

[Tutor] Understanding error in recursive function

2016-04-08 Thread Tom Maher
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 = anima