filter() + simple program not outputting
Charles G Waldman
cgw at fnal.gov
Mon Jul 26 17:47:15 EDT 1999
More information about the Python-list mailing list
Mon Jul 26 17:47:15 EDT 1999
- Previous message (by thread): filter() + simple program not outputting
- Next message (by thread): Python and Red Hat Linux 6
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
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.
- Previous message (by thread): filter() + simple program not outputting
- Next message (by thread): Python and Red Hat Linux 6
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
More information about the Python-list mailing list