Re: isPrime works but UnBoundLocalError when mapping on list

2008-07-16 Thread Terry Reedy
Andreas Tawn wrote: Terry Reedy wrote: For loop variables continue after the loop exits. This is intentional. I never knew that and I can't find reference to it in the docs. Can you help me with the reasons for it? 1. Python's reluctance to multiply scopes without necessity (as other re

Re: isPrime works but UnBoundLocalError when mapping on list

2008-07-16 Thread Fredrik Lundh
Andreas Tawn wrote: I don't have experience of too many other languages, but in C++ (and I guess C)... That's invalid C (you cannot declare variables in the "for" statement itself, at least not in C89). And back in the old days, some C++ compilers did in fact leak declarations from "for" lo

RE: isPrime works but UnBoundLocalError when mapping on list

2008-07-16 Thread Andreas Tawn
> Andreas Tawn wrote: > > Terry Reedy wrote: > >> Wrong. > > Thank you. > > > >> For loop variables continue after the loop exits. This is > >> intentional. > > I never knew that and I can't find reference to it in the docs. > > Interesting starting point. It never occurred to me > that they

Re: isPrime works but UnBoundLocalError when mapping on list

2008-07-16 Thread Tim Golden
Andreas Tawn wrote: Terry Reedy wrote: Wrong. Thank you. For loop variables continue after the loop exits. This is intentional. I never knew that and I can't find reference to it in the docs. Interesting starting point. It never occurred to me that they might not. (So I didn't look for an

Re: isPrime works but UnBoundLocalError when mapping on list

2008-07-16 Thread Fredrik Lundh
Andreas Tawn wrote: I never knew that and I can't find reference to it in the docs. the for-in loop does ordinary assignments in the current scope: http://docs.python.org/ref/for.html "Each item in turn is assigned to the target list using the standard rules for assignments, and

RE: isPrime works but UnBoundLocalError when mapping on list

2008-07-16 Thread Andreas Tawn
Terry Reedy wrote: >Wrong. Thank you. >For loop variables continue after the loop exits. This is >intentional. I never knew that and I can't find reference to it in the docs. Can you help me with the reasons for it? Drea -- http://mail.python.org/mailman/listinfo/python-list

Re: isPrime works but UnBoundLocalError when mapping on list

2008-07-15 Thread Terry Reedy
Andreas Tawn wrote: I think a better explanation is that in your original function, x only existed while the for loop was running. As soon as execution hit the break statement, x ceased to exist. Wrong. For loop variables continue after the loop exits. This is intentional. Mensanator gav

Re: isPrime works but UnBoundLocalError when mapping on list

2008-07-15 Thread norseman
Mensanator wrote: On Jul 15, 12:36 pm, defn noob <[EMAIL PROTECTED]> wrote: On Jul 15, 7:28 pm, Mensanator <[EMAIL PROTECTED]> wrote: On Jul 15, 11:26 am, defn noob <[EMAIL PROTECTED]> wrote: isPrime works when just calling a nbr but not when iterating on a list, why? adding x=1 makes it

Re: isPrime works but UnBoundLocalError when mapping on list

2008-07-15 Thread Mensanator
On Jul 15, 12:36 pm, defn noob <[EMAIL PROTECTED]> wrote: > On Jul 15, 7:28 pm, Mensanator <[EMAIL PROTECTED]> wrote: > > > > > > > On Jul 15, 11:26 am, defn noob <[EMAIL PROTECTED]> wrote: > > > > isPrime works when just calling a nbr but not when iterating on a > > > list, why? adding x=1 makes i

Re: isPrime works but UnBoundLocalError when mapping on list

2008-07-15 Thread Mensanator
On Jul 15, 12:28 pm, "Andreas Tawn" <[EMAIL PROTECTED]> wrote: > >defn noob wrote: > >> isPrime works when just calling a nbr but not when iterating on a > >> list, why? adding x=1 makes it work though but why do I have to add > >> it? > >> Is there a cleaner way to do it? > > >> def isPrime(nbr):

Re: isPrime works but UnBoundLocalError when mapping on list

2008-07-15 Thread defn noob
On Jul 15, 7:28 pm, Mensanator <[EMAIL PROTECTED]> wrote: > On Jul 15, 11:26 am, defn noob <[EMAIL PROTECTED]> wrote: > > > > > isPrime works when just calling a nbr but not when iterating on a > > list, why? adding x=1 makes it work though but why do I have to add > > it? > > Is there a cleaner wa

Re: isPrime works but UnBoundLocalError when mapping on list

2008-07-15 Thread Mensanator
On Jul 15, 11:26 am, defn noob <[EMAIL PROTECTED]> wrote: > isPrime works when just calling a nbr but not when iterating on a > list, why? adding x=1 makes it work though but why do I have to add > it? > Is there a cleaner way to do it? > > def isPrime(nbr): >     for x in range(2, nbr + 1): >    

RE: isPrime works but UnBoundLocalError when mapping on list

2008-07-15 Thread Andreas Tawn
>defn noob wrote: >> isPrime works when just calling a nbr but not when iterating on a >> list, why? adding x=1 makes it work though but why do I have to add >> it? >> Is there a cleaner way to do it? >> >> >> def isPrime(nbr): >> for x in range(2, nbr + 1): >> if nbr % x == 0: >>

Re: isPrime works but UnBoundLocalError when mapping on list

2008-07-15 Thread norseman
defn noob wrote: isPrime works when just calling a nbr but not when iterating on a list, why? adding x=1 makes it work though but why do I have to add it? Is there a cleaner way to do it? def isPrime(nbr): for x in range(2, nbr + 1): if nbr % x == 0: break if x == n