Mailing List Archive

filter() + simple program not outputting
This is a multi-part message in MIME format.
--------------3D046B24293BC159E2813737
Content-Type: text/plain; charset=us-ascii
Content-Transfer-Encoding: 7bit

Hello,

New to python but not to programming...can someone tell me why this
simple program is not outputting when i run from the BASH prompt....any
pointers would be helpful...thanking you in advance....

Joyce




#!/usr/bin/env python

def f(x):
return x%2 != 0 and x%3 != 0
filter(f, range(2, 25))
f( )



--------------3D046B24293BC159E2813737
Content-Type: text/x-vcard; charset=us-ascii;
name="joyce.vcf"
Content-Transfer-Encoding: 7bit
Content-Description: Card for Joyce Stack
Content-Disposition: attachment;
filename="joyce.vcf"

begin:vcard
n:Stack;Joyce
tel;work:415-591-3027
x-mozilla-html:FALSE
org:1fb.net inc
adr:;;1000 Sansome #350;San Francisco;CA;94111;
version:2.1
email;internet:joyce@netcard.com
fn:Joyce Stack
end:vcard

--------------3D046B24293BC159E2813737--
filter() + simple program not outputting [ In reply to ]
Joyce> can someone tell me why this simple program is not outputting
Joyce> when i run from the BASH prompt

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@mojam.com | http://www.musi-cal.com/~skip/
847-475-3758
filter() + simple program not outputting [ In reply to ]
(posted and mailed)

Joyce Stack <joyce@netcard.com> wrote:
> New to python but not to programming...can someone tell me why this
> simple program is not outputting when i run from the BASH prompt....any
> pointers would be helpful...thanking you in advance....
>
> Joyce
>
> #!/usr/bin/env python
>
> def f(x):
> return x%2 != 0 and x%3 != 0
> filter(f, range(2, 25))
> f( )

dunno. what is it supposed to do? there's
no print statement in there, nor anything
else related to printing. and while "f" is
declared, it's never called. and that
function looks a bit strange -- it starts
out by returning to the caller, and thus
never runs any code.

but it's close. could the following be
what you tried to do:

--- cut ---
#!/usr/bin/env python

def f(x):
return x%2 != 0 and x%3 != 0

print filter(f, range(2, 25))
--- cut ---

note the use of indentation -- things at
the left margin are executed when you
run this as a script ("def" is a statement
too!).

</F>
filter() + simple program not outputting [ In reply to ]
Joyce Stack writes:
> Hello,
>
> New to python but not to programming...can someone tell me why this
> simple program is not outputting when i run from the BASH prompt....any
> pointers would be helpful...thanking you in advance....
>
> Joyce
>
>
> #!/usr/bin/env python
>
> def f(x):
> return x%2 != 0 and x%3 != 0
> filter(f, range(2, 25))
> f( )
>
>

Well, there are several reasons! One is that you define a function
but never call it.

You probably meant to say:

> def f(x):
> return x%2 != 0 and x%3 != 0
>
> filter(f, range(2, 25))



having the "filter" inside the "def f" doesn't make much sense;
it will never be reached where it is (after a "return" statement).
I trust that you don't mean to be calling "f" recursively.

The final "f( )" makes no sense because f is defined as taking a
single argument. Delete this line.

Finally, there's a difference between running in interactive mode and
running a python file as a script. In interactive mode, values of
computed objects are printed by default. In a script, they are not.
That is, at the Python prompt, entering "2+2" will result in "4"
being printed, but if you create a file "test.py" containing only the
line "2+2" and do "python test.py", 2+2 will be calculated but not
printed. I believe that if you modify your example to something like:


> def f(x):
> return x%2 != 0 and x%3 != 0
>
> print filter(f, range(2, 25))

you will get what you intended.

Hope this helps.