Login or Sign up Help | Site Map
Connecting Tech Pros Worldwide

User input and classes

Question posted by: sirdeltalot (Newbie) on July 4th, 2008 09:21 PM
Hi,
I'm learning python and i'm in the process of trying to write an adress book program. I'm using a class to do it all and i'm hopeless at oop. Heres a bit of code:

Code: ( text )
  1. run = 1
  2. class Person:
  3.     pop = 0
  4.     def __init__(self, name):
  5.         self.name = name
  6.         Person.pop +=1
  7.     def sayHi(self):
  8.         print 'Hello, my name is', self.name
  9.     def countPeople(self):
  10.         print 'There are %d people here' % Person.pop
  11.     def delPerson(self):
  12.         del self.name
  13.         print 'Person deleted'
  14.         Person.pop -= 1
  15.  
  16. while run == 1:   
  17.     c = raw_input('Please enter a command:')
  18.  
  19.     if c == 'add':
  20.         p = Person(raw_input('Please enter a name:'))
  21.         p.sayHi()
  22.     elif c == 'del':
  23.         p.delPerson()
  24.     elif c == 'count':
  25.         p.countPeople()
  26.     elif c == 'break':
  27.         break

What i want to know is how to select which added person to delete (at the moment it automatically deletes the most recently added) and how to list all the people.
Thanks
Programming noob Iz
Would you like to answer this question?
Sign up for a free account, or Login (if you're already a member).
elcron's Avatar
elcron
Member
43 Posts
July 5th, 2008
01:44 PM
#2

Re: User input and classes
You're only storing the last created person. You could use a list to store all of the people created.

Code: ( text )
  1. run = 1
  2. class Person:
  3.     people = []
  4.     def __init__(self, name):
  5.         self.name = name
  6.         Person.people.append(self)
  7.     def sayHi(self):
  8.         print 'Hello, my name is', self.name
  9.     def countPeople(self):
  10.         print 'There are %d people here' % len(Person.people)
  11.     def delPerson(self):
  12.         Person.people.remove(self)
  13.         del self.name
  14.         print 'Person deleted'
  15.  
  16. while run == 1:   
  17.     c = raw_input('Please enter a command:')
  18.  
  19.     if c == 'add':
  20.         Person(raw_input('Please enter a name:'))
  21.         p = Person.people[-1]
  22.         p.sayHi()
  23.     elif c == 'del':
  24.         p.delPerson()
  25.         if Person.people:
  26.                 p = Person.people[-1]
  27.     elif c == 'count':
  28.         p.countPeople()
  29.     elif c == 'break':
  30.         break


Here p is alway that most recently created person but it wouldn't be to hard to add the option to change the current person.

Reply
sirdeltalot's Avatar
sirdeltalot
Newbie
7 Posts
July 7th, 2008
05:33 PM
#3

Re: User input and classes
Thanks. LOL, so obvious once your told it, ah well, my mind doesn't work as logically as that.

Reply
sirdeltalot's Avatar
sirdeltalot
Newbie
7 Posts
July 7th, 2008
07:45 PM
#4

Re: User input and classes
Thanks for you're help, but another problem has popped up-when i print a list (with things added to it) i just get
Code: ( text )
  1. <__main__.Person instance at 0x11bd5a8>
only telling me where it is, not what it is. How do i modify your code to make it append an item i can print off?
Thanks
Iz

Reply
Laharl's Avatar
Laharl
Expert
703 Posts
July 7th, 2008
10:24 PM
#5

Re: User input and classes
Define the __str__(self) method for the class, as that allows you (and print!) to call str() on objects of the class.

Code: ( text )
  1. class Test:
  2.     def __init__(self):
  3.         self.hello = "Hello World!"
  4.  
  5.     def __str__(self):
  6.         return self.hello
  7.  
  8. if __name__ == '__main__':
  9.     test = Test()
  10.     print test #Hello World!

Reply
Laharl's Avatar
Laharl
Expert
703 Posts
July 8th, 2008
01:16 AM
#6

Re: User input and classes
I forgot to add something here: Using print <list> doesn't call str() on the contents, something that continually annoys me. Thus, you should loop through the list and print each object in it.

Reply
elcron's Avatar
elcron
Member
43 Posts
July 8th, 2008
01:56 PM
#7

Re: User input and classes
You could overload the __repr__ function to fix that.

Code: ( text )
  1. >>> class C:
  2.     def __repr__(self):
  3.         return "Test"
  4.  
  5. >>> C
  6. <class __main__.C at 0x860e68c>
  7. >>> C()
  8. Test
  9. >>> [C(), C(), C()]
  10. [Test, Test, Test]

Reply
Reply
Not the answer you were looking for? Post your question . . .
182,081 Experts ready to help you find a solution.
Sign up for a free account, or Login (if you're already a member).

Top Python Forum Contributors