0

I'm making class for people and their names. I want to make specific name for each one like:

obj0, obj1, obj2... + obji

im trying to do this in iteration. Which sign could I use to connect 'obj' and 'i'?

for i in range(100):
    name=input('Your name:')
    obji=Person(name)
p0p0s
  • 3
  • 2

3 Answers3

0

Assuming you want to actually be pythonic (or really, just not insane enough to make 100 variables), you could make a list.

obj = []
for i in range(100):
    name=input('Your name:')
    obj.append(Person(name))

If you want to set the size of your list before your loop so you can access it with i, you can do something like this

obj = [None] * 100
for i in range(100):
    name=input('Your name:')
    obj[i] = Person(name)
Blair
  • 6,623
  • 1
  • 36
  • 42
0

Don't try to make variables like this on the fly, instead you can use a dictionary for example

people = {}
for i in range(100):
    name = input('Your name:')
    people[i] = name

Then you can look them up like

people[7]

Although if they are just going to be sequential, instead of a dict you could just use a list

people = []
for i in range(100):
    people.append(input('Your name: '))

You could then still look people up the same way

people[7]
Cory Kramer
  • 114,268
  • 16
  • 167
  • 218
0

rather than trying to create separate, related names, why not put them in a list:

objs=[]
for i in range(100):
    name = input("Your Name? ")
    objs.append(Person(name)
Alan Hoover
  • 1,430
  • 2
  • 9
  • 13