User input and classes

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • sirdeltalot
    New Member
    • Jun 2008
    • 7

    User input and classes

    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:
    run = 1
    class Person: 
        pop = 0
        def __init__(self, name): 
            self.name = name
            Person.pop +=1
        def sayHi(self): 
            print 'Hello, my name is', self.name
        def countPeople(self):
            print 'There are %d people here' % Person.pop
        def delPerson(self):
            del self.name
            print 'Person deleted'
            Person.pop -= 1
    
    while run == 1:   
        c = raw_input('Please enter a command:')
    
        if c == 'add':
            p = Person(raw_input('Please enter a name:')) 
            p.sayHi() 
        elif c == 'del':
            p.delPerson()
        elif c == 'count':
            p.countPeople()
        elif c == 'break':
            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
  • elcron
    New Member
    • Sep 2007
    • 43

    #2
    You're only storing the last created person. You could use a list to store all of the people created.

    [code=Python]run = 1
    class Person:
    people = []
    def __init__(self, name):
    self.name = name
    Person.people.a ppend(self)
    def sayHi(self):
    print 'Hello, my name is', self.name
    def countPeople(sel f):
    print 'There are %d people here' % len(Person.peop le)
    def delPerson(self) :
    Person.people.r emove(self)
    del self.name
    print 'Person deleted'

    while run == 1:
    c = raw_input('Plea se enter a command:')

    if c == 'add':
    Person(raw_inpu t('Please enter a name:'))
    p = Person.people[-1]
    p.sayHi()
    elif c == 'del':
    p.delPerson()
    if Person.people:
    p = Person.people[-1]
    elif c == 'count':
    p.countPeople()
    elif c == 'break':
    break
    [/code]

    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.

    Comment

    • sirdeltalot
      New Member
      • Jun 2008
      • 7

      #3
      Thanks. LOL, so obvious once your told it, ah well, my mind doesn't work as logically as that.

      Comment

      • sirdeltalot
        New Member
        • Jun 2008
        • 7

        #4
        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:
        <__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

        Comment

        • Laharl
          Recognized Expert Contributor
          • Sep 2007
          • 849

          #5
          Define the __str__(self) method for the class, as that allows you (and print!) to call str() on objects of the class.

          [code=python]
          class Test:
          def __init__(self):
          self.hello = "Hello World!"

          def __str__(self):
          return self.hello

          if __name__ == '__main__':
          test = Test()
          print test #Hello World!
          [/code]

          Comment

          • Laharl
            Recognized Expert Contributor
            • Sep 2007
            • 849

            #6
            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.

            Comment

            • elcron
              New Member
              • Sep 2007
              • 43

              #7
              You could overload the __repr__ function to fix that.

              Code:
              >>> class C:
              	def __repr__(self):
              		return "Test"
              
              >>> C
              <class __main__.C at 0x860e68c>
              >>> C()
              Test
              >>> [C(), C(), C()]
              [Test, Test, Test]

              Comment

              Working...