<span style="font-family: georgia;">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.
</span><br style="font-family: georgia;"><br style="font-family: georgia;"><span style="font-family: georgia;">Thanks in advance. </span><br style="font-family: georgia;"><span style="font-family: georgia;">Ben </span><br>
<br><br># File gloss.py<br># Author Ben Markwell<br># Date Feb 2006<br><br>'''<br>This script is a glossary in which you can input definitions<br>and look up words and their definitions.<br>'''<br><br>import cPickle as p
<br>import textwrap as t<br><br>f = file(r'g:\dev\python\data\gloss.data', 'r')<br>gloss = p.load(f)<br><br>#---Def Functions---#000000#FFFFFF--------------------------------------------------<br><br>def findWord():<br> words =
gloss.keys()<br> letter = raw_input('That word is not in the glossary. \nWhat is the first letter of the word? ')<br> for x in range(len(words)):<br> if words[x].startswith(letter):<br> print words[x]
<br><br># Get word and check if it is in the glossary then print the def.<br>def getWordDef():<br> word = raw_input('enter a word: ')<br> if gloss.has_key(word):<br> line = gloss.get(word)<br> lineWrap(word)
<br> else:<br> findWord()<br> word = raw_input('Enter the word: ')<br> line = gloss.get(word)<br> lineWrap(word)<br> <br># Print a menu of choices to the screen<br>def printMenu():<br>
print '1 - Enter a word and definition.'<br> print '2 - Look up a word.'<br> print '3 - Quit'<br> print<br><br># Wrap the output so it fits the screen nicely<br>def lineWrap(word):<br> line = gloss.get(word)
<br> if len(line) < 81:<br> print line<br> else:<br> wrapLines = t.wrap(line, 80)<br> for line in range(len(wrapLines)):<br> print wrapLines[line]<br> <br># End of Def Functions xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
<br><br>num = '0'<br>printMenu()<br>while num != '3':<br> num = raw_input('What do you want to do? ')<br> if num == '1': # if 1 in choice enter a word to look up<br> word = raw_input('Enter word: ')
<br> _def = raw_input('Enter definition: ')<br> gloss[word] = _def<br><br> elif num == '2': # if 2 is the choice enter word and def<br> getWordDef()<br><br> elif num == '3': # if 3 is the choice, save changes and quit
<br> newGloss = file(r'g:\dev\python\data\gloss.data', 'w')<br> p.dump(gloss, newGloss)<br> newGloss.close()<br> f.close()<br> print<br> print 'Goodbye'<br> break<br>
<br> else: <br> printMenu()<br><br>