I am not very experienced in coding as of yet, I have just begun my education in the subject. So I need some advice. I'm writing a program that is a sort of simulation of life (a very simple one). There are people (instances of class Person) which have attributes like name, age, gender (there will be more but I am starting small). So the idea is I, as the user, can run this program with a select number of years. Time is all I control. During this period events will happen: births, deaths, sickness, etc. The program will write out all the events in a txt file so you can see what happened in these peoples lives.
Thats the basic gist of it. My biggest issue right now, is I want automatic births and creation of people. But I don't know how to write a program that automatically creates variables. Usually you have be to like
p0001=Person('John','male',0)
^I can't figure out how to get the variables to make themselves and go so that each time a new person is created it would just go up like p0002, p0003 etc.
Any help would be appreciated, Thank you guys!:)
-Clint
Writing Automatic Instance of Class HELP!
Started by CarpeVexillum, Aug 09 2010 05:17 AM
6 replies to this topic
#1
Posted 09 August 2010 - 05:17 AM
|
|
|
#2
Posted 09 August 2010 - 08:20 AM
you can't create variables like that, use a list instead
#example Person class as you didn't supply it
class Person(object):
def __init__(self, name, gender, x):
self.name = name
self.gender = gender
self.x = x
def example_method(self, message):
print self.name + ': ' + message
#list of persons
persons = []
for i in range(10): #10 is the amount of persons
persons.append(Person('John', 'male', 0))
#access like this
print persons[0].name
print persons[1].gender
print persons[8].example('Hello, how are you?')
#3
Posted 09 August 2010 - 05:14 PM
Thank you for the timely response! I don't think I fully understand though, or maybe I didn't explain well enough.
Basically, at some point in the program, it will need to create one new Person. Its not something I can set up in the beginning because it has to happen only if certain conditions are met and once a person is created they have to be unique and have a unique variable name. I think to store all the info I would write it out to a txt file (one txt file per person).
I don't think the list solves this problem, but if it does, woops:-P
Basically, at some point in the program, it will need to create one new Person. Its not something I can set up in the beginning because it has to happen only if certain conditions are met and once a person is created they have to be unique and have a unique variable name. I think to store all the info I would write it out to a txt file (one txt file per person).
I don't think the list solves this problem, but if it does, woops:-P
#4
Posted 10 August 2010 - 01:46 AM
You can add a new instance of Person to the list persons at any point, using the.
read more about lists here: 5. Data Structures — Python v2.7 documentation
read more about lists here: 5. Data Structures — Python v2.7 documentation
#5
Posted 11 August 2010 - 07:08 AM
You could use another array with a tuple of the name, gender, and age.
or if you really wanted to create multiple variables you could use exec.
Hope this helps.
#example Person class as you didn't supply it
class Person(object):
def __init__(self, name, gender, x):
self.name = name
self.gender = gender
self.x = x
def example_method(self, message):
print self.name + ': ' + message
people_id = [('John', 'male', 200),
('Susan', 'female', 1)]
#etc
people = []
n = 0
for name, gender, age in people_id:
people[n] = Person(name, gender, age)
n += 1
#rest of program
or if you really wanted to create multiple variables you could use exec.
#example Person class as you didn't supply it
class Person(object):
def __init__(self, name, gender, x):
self.name = name
self.gender = gender
self.x = x
def example_method(self, message):
print self.name + ': ' + message
people_id = [("'John'", "'male'", 200),
("'Susan'", "'female'", 1)]
#etc
n = 0
for name, gender, age in people_id:
exec("p%d = Person(%s, %s, %d) %(n, name, gender, age))
n += 1
#rest of program
The extra quotes in the names are necessary.Hope this helps.
I C!(and Python, and C++, and ...)
#6
Posted 11 August 2010 - 08:00 AM
spyder, you can't assign a list index that doesn't exist, you need to use the list.append method ;)
OP you might also want to use dictionaries, which are associative arrays.
OP you might also want to use dictionaries, which are associative arrays.
#7
Posted 11 August 2010 - 08:40 AM
Good point.
I forgot about that, I just did a quick post.^^
I forgot about that, I just did a quick post.^^
I C!(and Python, and C++, and ...)


Sign In
Create Account

Back to top









