[Tutor] functions, system statements, sleep

alan.gauld@bt.com alan.gauld@bt.com
Fri, 19 Jan 2001 17:08:24 -0000


> I'm a complete idiot, 

No you're a beginner that's all. Different concepts :-)

> but when I create a function in Python, 
> how do I run it? Say I create a function that has a main 
> menu. And after the user goes through the introductory, how 
> can I run the main_menu function?

Are you still using the python >>> prompt?

If so you just type the name of the function with parentheses, 
possibly comntaining some argument values.

>>> main_menu()

If on the other hand your function is in a file called menu.py
and you are writing a program that uses it you will have to

>>> import menu

first then call it by prepending the module name

>>> menu.main_menu()

If you are writing you main program in a file called myprog.py
then within that you would define main_menu then call it:

def main_menu():
   # do whatever you do

# do some other stuff

main_menu() # call main_menu

OR if main_menu is in menu.py:

import menu
menu.main_menu()

> How can I get the python interpreter to run a Unix system 
> function? Like say I want it to use 'clear' 

import os
os.system('clear')

Noe that this won't return any values so something like 
cat, ls etc will just display to stdout, not give you any 
results back. For that you need popen()...

> And is there a sleep function in Python, that will make it 
> wait for a couple of seconds before doing anything? 

import time
time.sleep(2)

> one in Perl, and I find it really useful.

Most Perl functions are available in Python but they will 
be found in a module rather than builtin a Perl does it. 
If in doubt look in the Python documentation in the Standard 
Library Reference index.

HTH,

Alan G.