473,406 Members | 2,745 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

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:

Expand|Select|Wrap|Line Numbers
  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
Jul 4 '08 #1
6 1815
elcron
43
You're only storing the last created person. You could use a list to store all of the people created.

Expand|Select|Wrap|Line Numbers
  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
  31.  
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.
Jul 5 '08 #2
Thanks. LOL, so obvious once your told it, ah well, my mind doesn't work as logically as that.
Jul 7 '08 #3
Thanks for you're help, but another problem has popped up-when i print a list (with things added to it) i just get
Expand|Select|Wrap|Line Numbers
  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
Jul 7 '08 #4
Laharl
849 Expert 512MB
Define the __str__(self) method for the class, as that allows you (and print!) to call str() on objects of the class.

Expand|Select|Wrap|Line Numbers
  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!
  11.  
Jul 7 '08 #5
Laharl
849 Expert 512MB
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.
Jul 8 '08 #6
elcron
43
You could overload the __repr__ function to fix that.

Expand|Select|Wrap|Line Numbers
  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] 
Jul 8 '08 #7

Sign in to post your reply or Sign up for a free account.

Similar topics

5
by: bart plessers | last post by:
Hello, Somewhere in my code I have <input TYPE="button" NAME="btnFirst" VALUE="<<" OnClick="GetFile('1')" DISABLED> I changed the layout of the INPUT with a stylesheet to INPUT { color:...
2
by: SophistiCat | last post by:
Hi, I am working on a computational program that has to read a number of parameters (~50) from an input file. The program contains a single class hierarchy with about a dozen member-classes or...
8
by: FS Liu | last post by:
Hi, I am writing ATL Service application (XML Web service) in VS.NET C++. Are there any sample programs that accept XML as input and XML as output in the web service? Thank you very much.
1
by: Ani | last post by:
Hi, I need to carry the user input across pages and then at the end insert all the values into the DB. How do I best accomplish this task in ASP.Net. I am a novice , please give me some simple...
8
by: Jackson | last post by:
I want a class that will determine its base class by the argument passed in. What I am about to write _does_not_work_, but it shows what I am trying to do. class ABC(some_super): def...
7
by: nemo | last post by:
Try to explain this as simple as I can - User provides a list (names, ID's, or ....) into a text box on a GUI application... Based on which options they select I have to look up the...
5
by: no1zson | last post by:
I have been reading through many of the array questions and cannot find one that addresses my issue. Maybe someone can help me out. Same story, I am learning Java and have just written a CD...
2
by: =?Utf-8?B?bXVzY2xla2luZw==?= | last post by:
say i have a form with many input text boxes, what is the proper way to store these values so when the next time the user opens the form, all the input from last session remains in the form? people...
30
by: Yorian | last post by:
Hey, Although I've been using classes and object for quite a while now I've never actually programmed proper OO code yet. It ofcourse depends on what you call proper OO code. I have been...
0
by: 703designs | last post by:
I'm with FutureShock, both of you guys got a little too heated. But it livened up my midday a bit, for what it's worth. Thomas On Nov 10, 10:02 pm, FutureShock <futuresho...@att.netwrote:
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.