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 )
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
Would you like to answer this question?
Sign up for a free account, or Login (if you're already a member).
|
|
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 )
run = 1 class Person: people = [] def __init__(self, name): self.name = name Person.people.append(self) def sayHi(self): print 'Hello, my name is', self.name def countPeople(self): print 'There are %d people here' % len(Person.people) def delPerson(self): Person.people.remove(self) del self.name print 'Person deleted' while run == 1: c = raw_input('Please enter a command:') if c == 'add': Person(raw_input('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
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.
|
|
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.
|
|
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 )
<__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
|
|
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 )
class Test: def __init__(self): self.hello = "Hello World!" def __str__(self): return self.hello if __name__ == '__main__': test = Test() print test #Hello World!
|
|
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.
|
|
July 8th, 2008 01:56 PM
# 7
|
Re: User input and classes
You could overload the __repr__ function to fix that.
Code: ( text )
>>> class C: def __repr__(self): return "Test" >>> C <class __main__.C at 0x860e68c> >>> C() Test >>> [C(), C(), C()] [Test, Test, Test]
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
|