Re: [Tutor] Tutor Digest, Vol 146, Issue 9

2016-04-09 Thread khalil zakaria Zemmoura
Hi,
there is one place i think you have to have a look at it and it's the
python official site.

One thing i love about python is the so well written documentation, i
remember when i was learning ruby and had to go to the official doc! it was
hard to understand for the beginner how i was. so, here is the link
https://docs.python.org/3/tutorial/index.html

Good luck

Regards
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


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 isn't. In other langauges, the function would call itself
until you run out of memory, but in Python there is an (adjustable) limit
to the number of recursion that can be executed, before the error occurs
that you describe.


One simple rewrite of your function that counts alle elements of all lists
in the dictionary (I am not sur if that's what you want, but I guess you
can figure out what you need from the example) is this: http://goo.gl/frZfkc

animals = { 'a': ['aardvark'], 'b': ['baboon'], 'c': ['coati']}
x = animals['a']

def how_many(aDict):
  count = 0
  for value in animals.values():
   count += len(value)
  return count

print(how_many(x))

(hope the pasted code is not mangled ...)

cheers
Oliver

On Sat, 9 Apr 2016 at 00:52 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
> ___
> Tutor maillist  -  Tutor@python.org
> To unsubscribe or change subscription options:
> https://mail.python.org/mailman/listinfo/tutor
>
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


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 function 'howMany' again and so on!

before the for loop there is nothing happening to the function you defined
or the argument is not modified in the recursive call, so i don't
understand why you used a recursion there!

i think you have to replace the howMany(aDict) in the for loop by aDict it
self and it will be ok.

for value in aDict: ...

Regards
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


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': ['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

___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor