Showing posts with label menu. Show all posts
Showing posts with label menu. Show all posts

Thursday, May 28, 2009

A simple console program

Say you want to make your own console program. This program should be able of recording data you enter to a text file, and extracting data from the text file to analyze them. One such program can be a tool to register values of different variables you are measuring, such as the number of visitors you have had in your office today. Say you count office visits, and you want to store this in the file under the name "office". Then, you also count how many times during the day your phone rings. This variable is called "phone". You create a program that pops up this menu:

1. Register KPI
2. Create KPI report
3. Quit
>>

When you choose 1, you are prompted for name an value of variable.

The menu is displayed when a function called mainmenu is called. Such a menu can be written as follows in Python:

def mainmenu():
print '1: Register KPI'
print '2: Create KPI report'
print '3: Quit'
menuChoice = raw_input('>> ')

Depending on the input the user gives, we cann call different functions (still within mainmenu):

if menuChoice == '1':
regKPI()
elif menuChoice == '2':
createReport()
elif menuChoice == '3':
print 'Goodbye'
else:
print 'Not a valid menu item. Pleast try again'
main()

There you go, a working menu. Next time: we show what the functions regKPI and createReport look like :-)