Here is my first stab at putting together a working program. It is a glossary that you can add words and definitions to, or look up words and their definitions.  There are a couple of problems here and there, but it basically does what I set out for it to do. All comments and criticisms are welcome. I attached the data file that goes with the script in case anybody wanted to actually run it, so they wouldn't have to make their own dictionary file to access.

Thanks in advance. 
Ben


# File      gloss.py
# Author    Ben Markwell
# Date      Feb 2006

'''
This script is a glossary in which you can input definitions
and look up words and their definitions.
'''

import cPickle as p
import textwrap as t

f = file(r'g:\dev\python\data\gloss.data', 'r')
gloss = p.load(f)

#---Def Functions---#000000#FFFFFF--------------------------------------------------

def findWord():
    words =  gloss.keys()
    letter = raw_input('That word is not in the glossary. \nWhat is the first letter of the word? ')
    for x in range(len(words)):
        if words[x].startswith(letter):
            print words[x]

# Get word and check if it is in the glossary then print the def.
def getWordDef():
    word = raw_input('enter a word: ')
    if gloss.has_key(word):
        line = gloss.get(word)
        lineWrap(word)
    else:
        findWord()
        word = raw_input('Enter the word: ')
        line = gloss.get(word)
        lineWrap(word)
        
# Print a menu of choices to the screen
def printMenu():
    print '1 - Enter a word and definition.'
    print '2 - Look up a word.'
    print '3 - Quit'
    print

# Wrap the output so it fits the screen nicely
def lineWrap(word):
    line = gloss.get(word)
    if len(line) < 81:
        print line
    else:
        wrapLines = t.wrap(line, 80)
        for line in range(len(wrapLines)):
            print wrapLines[line]
            
# End of Def Functions xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx

num = '0'
printMenu()
while num != '3':
    num = raw_input('What do you want to do? ')
    if num == '1':            # if 1 in choice enter a word to look up
        word = raw_input('Enter word: ')
        _def = raw_input('Enter definition: ')
        gloss[word] = _def

    elif num == '2':          # if 2 is the choice enter word and def
        getWordDef()

    elif num == '3':          # if 3 is the choice, save changes and quit
        newGloss = file(r'g:\dev\python\data\gloss.data', 'w')
        p.dump(gloss, newGloss)
        newGloss.close()
        f.close()
        print
        print 'Goodbye'
        break
        
    else:
        printMenu()

_______________________________________________
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor

Reply via email to