[Tutor] First program -- would like comments and criticisms

Kent Johnson kent37 at tds.net
Mon Feb 20 12:03:47 CET 2006


benmarkwell at gmail.com wrote:
> 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.

See my suggestions below.

Kent

> 
> 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)

f.close() should be here, you're done with reading this file now.

> 
> #---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]

To iterate over the elements of a sequence you don't need to use an 
index, you can iterate the sequence directly:
for word in words:
   if word.startswith(letter):
     print word

Since words isn't used anywhere else I would write this as
for word in gloss.keys():

Finally, since you just asked for the word (if I understand correctly 
the intent), it would be friendlier to figure out the first letter 
yourself and pass it as a parameter to findWord(), rather than having 
the user enter it again.
> 
> # 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]

for wrapped in t.wrap(line, 80):

>             
> # 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 at python.org
> http://mail.python.org/mailman/listinfo/tutor




More information about the Tutor mailing list