Forum Archive

Custom Module

reefboy1

Is it possible to program a module? Or even import it?
If so, how?

ccc
  • Put your code into a module called reefrunner (Pythonista will add the .py for you).
  • In your main program add the line: import reefrunner ; reload(reefrunner).
  • In your main program you can now use classes, functions, and variables defined in reefrunner.

The reload() command above is strongly advised for modules that you are editing to ensure that Pythonista is always using the latest and greatest code.

reefboy1

Thank you very much

reefboy1

I get an error that says no module named reefrunner

reefboy1

Never mind I figured it out

reefboy1

Ok another question.

import ReefRunner ; reload(ReefRunner)
gac = raw_input(' ')
def graphics(): 
    gbox=''' |-------------------|
                     |                   |
                     |                   |
                     |                   |
                     |                   |
                     |___________________|
                '''
if gac == 'graphics.gbox()':
    print(gbox)
else: 
    print('The Text You Entered Is Not Compatible With This Module')

It says gbox is not defined. What do I do?

ccc

In your code above, the scope of gbox is only inside of the function graphics().

If you indent the if statement to be inside of the function graphics(), and you call graphics(), it should work.

reefboy1

Thanks! Your super helpful! Are you like a moderator?

dgelessus

As far as I know only @omz (developer of Pythonista) has admin rights on the forums. @ccc just has a good amount of experience with Python. (Not as if this forum would even need moderators. Unlike a few other places on the internet, this is a very friendly and civil forum.)

reefboy1

I indented the if statment. But now it won't even print the box. I get no errors

ccc

You must call graphics() to run its code.

reefboy1

How do I call graphixs()

ccc

See the last line:

import ReefRunner ; reload(ReefRunner)

gac = raw_input('Type "grapics()" or not.: ').lower()
def graphics(): 
    gbox=''' |-------------------|
                     |                   |
                     |                   |
                     |                   |
                     |                   |
                     |___________________|
                '''
    if gac == 'graphics()':
        print(gbox)
    else: 
        print('The Text You Entered Is Not Compatible With This Module')

graphics()  # here we are calling graphics() to run the code of that function
reefboy1

Ok thanks. Sorry that was a noob question