#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2005
    Location
    U.S A.Z.
    Posts
    24
    Rep Power
    0

    Shelve data not displayed as expected.


    The following is a program I wrote just as a personal learning project. But I do not understand why when I use my display_all function that the first line is as expected but then the next line says None then the following line is as expected and so on. Does any one know why my program is doing this? I am using python 2.7.3. Also any other input would be appreciated.
    Code:
    import shelve
    
    
    class Phonedir():
    	def __init__(self,fname,lname,number):
    		self.fname = fname
    		self.lname = lname
    		self.number = number
    		self.fullname = lname +'_' + fname
    	def display(self):
    		print ('First: %s Last: %s Phone: %s') %(self.fname, self.lname, self.number)
    	
    	
    def menu():
    	print '1. Add Contact'
    	print '2. Search contacts'
    	print '3. Display all contacts'
    	print '4. Delete contact'
    	print '5  Quit'
    	menu = raw_input('Please enter a menu choice: ')
    	if menu == '1':
    		add_contact()
    	if menu == '2':
    		search_contact()
    	if menu == '3':
    		display_all()
    	if menu == '4':
    		delete_contact()
    	if menu == '5':
    		exit()
    
    def add_contact():
    
    	lname = raw_input('Last name: ')
    	fname = raw_input('First name: ')
    	number = raw_input('Number: ')
    	whole_name = (lname + '_' + fname)
    	whole_name = Phonedir(fname,lname,number)
    	
    	db = shelve.open('knutphonedir.db')
    	db[whole_name.fullname] = whole_name
    	db.close()
    	
    def search_contact():
    	print 'Please enter last name then first name to delete'
    	lname_del = raw_input('Last name: ')
    	fname_del = raw_input('First name: ')
    	fullname_del = lname_del + '_' + fname_del
    	db = shelve.open('knutphonedir.db')
    	if fullname_del in db.keys():
    		
    		db[fullname_del].display()
    	else:
    		print '\nNot Found\n'
    def display_all():
    
    	db = shelve.open('knutphonedir.db')
    	for x in db.keys():
    		print db[x].display()
    	print db.keys() # edit this out later
    	for x in db.keys():
    		print db[x].display()
    		
    	db.close()
    def delete_contact():
    	print 'Please enter last name then first name to delete'
    	lname_del = raw_input('Last name: ')
    	fname_del = raw_input('First name: ')
    	fullname_del = lname_del + '_' + fname_del
    	db = shelve.open('knutphonedir.db')
    	if fullname_del in db.keys():
    		print 'Found!'
    		db[fullname_del].display()
    		print 'The above contact will be deleted... ARE YOU SURE?'
    		choice = raw_input('(Y)es or (N)o: ')
    		choice = choice.upper()
    		
    		if choice == 'Y':
    			del db[fullname_del]
    			print' Contact deleted'
    		else:
    			print 'Deletion aborted'
    			
    	else:
    		print 'Not found'
    	db.close()
    
    while True:
    	menu()
  2. #2
  3. Contributing User
    Devshed Demi-God (4500 - 4999 posts)

    Join Date
    Aug 2011
    Posts
    4,837
    Rep Power
    480
    The db.display method prints to stdout and returns None. Then you printed the None. (The default return value from python callables is None .)
    Code:
    def display_all():
        db = shelve.open('knutphonedir.db')
        for x in db.keys():
            db[x].display()
        print db.keys() # edit this out later
        for x in db.keys():
            db[x].display()
        db.close()

    Comments on this post

    • knutrainer agrees
    [code]Code tags[/code] are essential for python code and Makefiles!
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2005
    Location
    U.S A.Z.
    Posts
    24
    Rep Power
    0
    Ahhh thanks! You sure are a huge help around here.

IMN logo majestic logo threadwatch logo seochat tools logo