Python Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me

The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.

Go Back   Dev Shed ForumsProgramming LanguagesPython Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old December 10th, 2012, 10:54 PM
knutrainer knutrainer is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jun 2005
Location: U.S A.Z.
Posts: 24 knutrainer User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 13 h 14 m 44 sec
Reputation Power: 0
Send a message via AIM to knutrainer
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()

Reply With Quote
  #2  
Old December 11th, 2012, 12:15 AM
b49P23TIvg's Avatar
b49P23TIvg b49P23TIvg is offline
Contributing User
Dev Shed Loyal (3000 - 3499 posts)
 
Join Date: Aug 2011
Posts: 3,458 b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level) 
Time spent in forums: 1 Month 2 Weeks 4 Days 6 h 33 m 33 sec
Reputation Power: 403
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!

Reply With Quote
  #3  
Old December 11th, 2012, 12:25 AM
knutrainer knutrainer is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jun 2005
Location: U.S A.Z.
Posts: 24 knutrainer User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 13 h 14 m 44 sec
Reputation Power: 0
Send a message via AIM to knutrainer
Ahhh thanks! You sure are a huge help around here.

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesPython Programming > Shelve data not displayed as expected.

Developer Shed Advertisers and Affiliates



Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap