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