Reputation: 335
Just a quick question. I've tried Googling this but every time you add "class" to a Python search it gives you classes on Python. D:
I've found threads like this: How can you dynamically create variables via a while loop?
But I don't think that's exactly what I'm looking for since concatenation is also part of the variable.
Here's my while loop:
def main():
counter = 1
while counter <= 3:
print "Name: ",employee[counter].name
print "ID Number: ",employee[counter].id_number
print "Department: ",employee[counter].department
print "Job Title: ",employee[counter].job_title
print #LB
counter = counter + 1
main()
I have my objects set up as:
employee1 = Employee("Susan Meyers", 47899, "Accounting", "Vice President")
employee2 = Employee("Mark Jones", 39119, "IT", "Programmer")
employee3 = Employee("Joy Rogers", 81774, "Manufacturing", "Engineer")
This is the error I receive:
print "Name: ",employee[counter].name
NameError: global name 'employee' is not defined
Obviously I have a class set up as well, but I don't think I need to paste that for this. What I want it to do is go through the loop and change the counter from 1 to 2 to 3. That way it'll loop through all three employees.
Is it possible to do that? I've only ever done this with arrays so I'm not 100% sure I'm on the right track.
Any tips would be great. <3
Chelsea
Upvotes: 1
Views: 2096
Reputation: 34718
I think what you are looking for is a list
employees = []
for i in xrange(1,10):
employees.append(employee("some name %s" % i))
employees[4].name
Upvotes: 1