#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

    Help storing object with the shelve module


    When I try to run the add_contact Function I get an error. I am trying to get info from the user and store it using the shelv module. I am using python 2.7.3 Any help worth exactly two brownie points.
    Code:
    File "knutphonedir.py", line 42, in add_contact
        for object in (whole_name):
    TypeError: iteration over non-sequence
    Code:
    import shelve
    
    
    class Phonedir():
    	def __init__(self,fname,lname,number):
    		self.fname = fname
    		self.lname = lname
    		self.number = number
    
    	def display(self):
    		print (self.fname, self.lname, self.number)
    	
    	
    
    def add_contact():
    
    	lname = raw_input('Last name: ')
    	fname = raw_input('First name: ')
    	number = raw_input('Number: ')
    	whole_name = (lname + ' ' + fname)
    	whole_name = Phonedir(lname,fname,number)
    	
    	db = shelve.open('knutphonedir.db')
    	for object in (whole_name):
    		db[object.lname] = object
    	db.close()
  2. #2
  3. Lord of the Dance
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Oct 2003
    Posts
    3,663
    Rep Power
    1958
    You assign whole_name with a string (consisting of the name) and then a Phonedir object.
    What will be the correct/expected value/type?

    What exactly do you want the for-loop iteration to do?

    The error happens because the Phonedir class doesn't have any knowledge/implement about iteration.

    Comments on this post

    • knutrainer agrees : Asking me these questions led me to solve my problem.
  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
    Thank you for your help. The key to finding asnwers is by asking the right questions.

IMN logo majestic logo threadwatch logo seochat tools logo