[Tutor] Moduling

Benoit Dupire bdupire@seatech.fau.edu
Tue, 24 Apr 2001 21:57:51 -0400


Your function 'times' is ok!
It takes a single argument, do some processing (here, it prints smtg on
the screen) and returns None (i.e nothing)
If you want to return something use the 'return' instruction:

def times (n):
    for i in range(1, 15):
        my_string ="%d x %d = %d" % (i, n, i*n)
    return my_string

I assume you defined the 'times' function in a file called 'times.py'

So the function 'times' lives in the module which has the same name
'times'.

To execute this function times from the Python interpreter or from
another module ( timesrun.py, for example  ;o) ),
you need to import times.
Then you have to call the function, and to tell Python in which module
it lives. You can do it like this...

import times
times.times(8)

You prepend the name of the function with the name of the module...

In This way you can have different times( ) functions in different
modules...
There is no colision between the different times( ) function
names....Each module defines a new namespace....

you don't need the 'print' statement because the printing is done inside
the function. The function only returns None, so if you do
print times.times(8)
then it would print None...


For the 2nd problem the syntax of if.. elif ... else.. is the following:

if  v>7:
    blabla code...
elif v=5:
    blabla code...
elif v<4:
    blabla code
else:
    bla bla code                # code executed  if  v=4, 6, 7


hope it helps,
Benoit





tyson toussaint wrote:

> def times (n):
>     for i in range(1, 15):
>         print "%d x %d = %d" % (i, n, i*n)
>
> import times
> print times(8)
>
> IDLE 0.6 -- press F1 for help
> >>>
> Traceback (innermost last):
>   File "<string>", line 1, in ?
>   File "A:\PythonFiles\timesrun.py", line 2, in ?
>     print times(8)
> TypeError: call of non-function (type module)
>
> def isPostitive(v):
>     if v >= 7:
>         return 1
>     elif:
>         return 0
>
> import isPositive
> print isPositive(4)
>
> Traceback (innermost last):
>   File "<string>", line 1, in ?
>   File "A:\PythonFiles\positive2run.py", line 1, in ?
>     import isPositive
>   File "A:\PythonFiles\isPositive.py", line 4
>     elif:
>         ^
> SyntaxError: invalid syntax
>
> Thanks a lot for responding Benoit.
>
> You should see two sets of problems: one for the
> function "times"; the second for the function
> "isPositive."  In both cases, I've provided all the
> data I have.  In all cases, I've tried to save
> everything on the A-drive to reduce any PythonPath
> problems.
>
> As you can see, I'm very lost and would appreciate any
> direction. (Right now "Go to hell" would be more
> direction than I have.)
>

--
Benoit Dupire
Graduate Student
----------------
I'd like to buy a new Boomerang. How can i get rid of the old one?