[Tutor] Input

moore william red_necks25 at yahoo.com
Mon Dec 29 16:07:29 EST 2003


I am trying to create a phone book. I have most of it
done at this point and am now tryign to make it work!
LOL. The problem is I am looking for the script to mak
the program look up names, remember input, and find
names in the data base. My program is capable of
puttign in the input and quiting right now. Here is
what I have so far:

import shelve
import string

def print_menu():
        print 'Welcome to the Phone Roster'
	print '1. Add Name'
	print '2. Delete Name'
	print '3. Look Up Name'
        print '9. Quit'
        print

numbers = {}
menu_choice = 0
print_menu()
while menu_choice != 9:
    menu_choice = input("Type in a number (1-9):")
    if menu_choice == 1:
        print "Add Number:"
        Name = raw_input ("Name:")
        Work = raw_input ("Work Number:")
        Home = raw_input ("Home Number:")
        FAX = raw_input ("Fax Number:")
        Cell = raw_input ("Cell Number:")
               
    elif menu_choice == 2:
        print "Delete Name;"
        Name = raw_input ("Name:")

    elif menu_choice == 3:
        print "Look Up Name;"
        Name = raw_input ("Name:")
        print "Name: "'x'" \tNumber: ",numbers[x]
        print
        
UNKNOWN = 0
HOME = 1
WORK = 2
FAX = 3
CELL = 4

class phoneentry:
	def __init__(self, name = 'Unknown', number =
'Unknown',
			type = UNKNOWN):
		self.name = name
		self.number = number
		self.type = type

	#  create string representation
	def __repr__(self):
		return('%s:%d' % ( self.name, self.type ))

	#  fuzzy compare or two items
	def __cmp__(self, that):
		this = string.lower(str(self))
		that = string.lower(that)

		if string.find(this, that) >= 0:
			return(0)
		return(cmp(this, that))

	def showtype(self):
		if self.type == UNKNOWN: return('Unknown')
		if self.type == HOME: return('Home')
		if self.type == WORK: return('Work')
		if self.type == FAX: return('Fax')
		if self.type == CELL: return('Cellular')

class phonedb:
	def __init__(self, dbname = 'phonedata'):
		self.dbname = dbname;
		self.shelve = shelve.open(self.dbname);

	def __del__(self):
		self.shelve.close()
		self.shelve = None

	def add(self, name, number, type = HOME):
		e = phoneentry(name, number, type)
		self.shelve[str(e)] = e

	def lookup(self, string):
		list = []
		for key in self.shelve.keys():
			e = self.shelve[key]
			if cmp(e, string) == 0:
				list.append(e)

		return(list)
		
#  if not being loaded as a module, run a small test
if __name__ == '__main__':
	foo = phonedb()
	foo.add('Sean Reifschneider', '970-555-1111', HOME)
	foo.add('Sean Reifschneider', '970-555-2222', CELL)
	foo.add('Evelyn Mitchell', '970-555-1111', HOME)

	print 'First lookup:'
	for entry in foo.lookup('reifsch'):
		print '%-40s %s (%s)' % ( entry.name, entry.number,
entry.showtype() )
	print

	print 'Second lookup:'
	for entry in foo.lookup('e'):
		print '%-40s %s (%s)' % ( entry.name, entry.number,
entry.showtype() )

# end of phonedb.py

__________________________________
Do you Yahoo!?
New Yahoo! Photos - easier uploading and sharing.
http://photos.yahoo.com/



More information about the Tutor mailing list