filter() + simple program not outputting

Skip Montanaro skip at mojam.com
Mon Jul 26 17:07:03 EDT 1999


    Joyce> can someone tell me why this simple program is not outputting
    Joyce> when i run from the BASH prompt

    Joyce> #!/usr/bin/env python
    Joyce> def f(x):
    Joyce>         return x%2 != 0 and x%3 != 0
    Joyce>         filter(f, range(2, 25))
    Joyce>         f( )

You need to call f from the module level (the calls to filter and f above
are from inside the scope of the function f).  Try:

    #!/usr/bin/env python
    def f(x):
	    return x%2 != 0 and x%3 != 0
    print filter(f, range(2, 25))

instead.

Skip Montanaro	| http://www.mojam.com/
skip at mojam.com  | http://www.musi-cal.com/~skip/
847-475-3758




More information about the Python-list mailing list