[Tutor] scope problem?

Kristoffer Erlandsson krier115@student.liu.se
Tue May 13 20:21:02 2003


On Tue, May 13, 2003 at 08:02:29PM -0400, David Broadwell wrote:
> Peter Jakubowicz wrote:
> > But haven't I defined it in line 2?
> Yes inside sieve. But outside sieve prime is undefined unless you assign it
> or return it.
> 
> THIS:
> prime = []
> def sieve(n):
>      prime = [2]
>      for i in range(2, n + 1):
>          for j in range(2, n + 1):
>              if i % j == 0:
>                  pass
>              else:
>                  prime.append(i)
> print prime

Actually, this won't work either. The prime that is used inside the
function is a new, local, prime. You need to insert the line

global prime

first in the function for it to use the global one.

Regards,
Kristoffer

-- 
Kristoffer Erlandsson
E-mail:  krier115@student.liu.se
ICQ#:    378225