0

I want to input data in a class. I don't know the number of students I want to input. I can only write p1.name="John" p2.name="Jack" etc but if I want to input more students I have to write p3,p4,p5...

class Person:
  def __init__(self, name, age):
    self.name = name
    self.age = age

p1 = Person
p1.name="John"
p1.age=15
print(p1.name)
print(p1.age)

Is there a method to work like with arrays for example p[0].name="John"....... p[123].name="Jack" or something like that... Sorry for my bad english

Tek Nath Acharya
  • 1,676
  • 2
  • 20
  • 35
  • Does this answer your question? [How do I create a variable number of variables?](https://stackoverflow.com/questions/1373164/how-do-i-create-a-variable-number-of-variables) – G. Anderson Jun 01 '20 at 16:23

4 Answers4

0

You probably meant

p1 = Person('John', 15)
p2 = Person('Jack', 42)

If you needed an arbitrary number of objects, you can store them in a data structure like

people = [Person('John', 15), Person('Jack', 42)]
for person in people:
  print(person.name)

You can read data into that array as well-

people = []
with open('people.txt') as f:
  for line in f:
    name, age = line.split()
    people.append(Person(name, age))

But my advice would be to find a more introductory tutorial to follow, you're going to struggle :)

Paul Becotte
  • 9,767
  • 3
  • 34
  • 42
  • Thanks for answer.. But what if i want to input when running program for example 50 persons: p[0].name=input() p[1].name=input() Can i do that? – Ovidiu Domanciuc Jun 01 '20 at 16:51
0

Sounds like you need a list.

A list is a data structure that you can use for zero or more elements, and you can access each element by iteration or by indexing the list.

You can do something like this:

persons = [
    Person("John", 15),
    Person("Adele", 16),
    ...
]

and then you can access each person by an index: persons[0] will give you Jonh, and persons[1] will give Adele.

jvd
  • 764
  • 4
  • 14
  • Thanks for answer but when i print persons[0] it give me "<__main__.Person object at 0x00AFF130>" – Ovidiu Domanciuc Jun 01 '20 at 16:49
  • That's because you basically print "a pointer" to the instance of Person. If you want to print a Person, you need to define either `def __str__(self)` or def `__repr__(self)` for an object, so that the print function knows what string to print. – jvd Jun 01 '20 at 16:52
0

if your objective is to store the students in an array-like structure, then you can definitely do this in Python. What you're currently doing is instantiating a new class object for each student. While this is totally valid, it may not be appropriate for the task you're trying to achieve.

Before mastering classes, I'd recommend familiarising yourself with python data structures like dictionaries, lists and tuples.

joshi123
  • 835
  • 2
  • 13
  • 33
0

Just another idea. You could store all of the students in a dictionary that is contained within a class. Wrapping the dictionary in a class will let you add or fetch students very easily.

class Person:
  def __init__(self, name, age):
    self.name = name
    self.age = age

class Classroom:
    def __init__(self):
        self.students = {}

    def add_student(self, student):
        self.students[student.name] = student

    def get_student(self, name):
        return self.students[name]

classroom = Classroom();
classroom.add_student(Person("John", 12))
classroom.add_student(Person("Sarah", 13))

print(classroom.get_student("Sarah").age)

#Output
#13
def main():
    classroom = Classroom()
    n = input("How many students would you like yo add? ")
    for i in range(int(n)):
        classroom.add_student(Person(input("Enter Name: "), int(input("Enter Age: "))))

ptan9o
  • 174
  • 9
  • n=input("How many students you want to input?: ") for i in range(0,n): classroom.add_student(Person(p[i], a[i])) If i change a little bit i get 2 errors: for i in range(0,n): TypeError: 'str' object cannot be interpreted as an integer 2)IndexError: list index out of range – Ovidiu Domanciuc Jun 02 '20 at 10:20
  • @OvidiuDomanciuc the input command will read anything as a string. So before you can do `range(0, n)` you need to cast n to an int `range(0, int(n))`. Check the update above. – ptan9o Jun 02 '20 at 15:30
  • Thx very much, it works but how can i acces a certain student?? For example student number 10 or 7 – Ovidiu Domanciuc Jun 02 '20 at 20:46
  • @OvidiuDomanciuc You need to decide how you want to access the various students in your list, or class, or dictionary. Whatever data structure you want to use. The method that I've dictated in the code above lets you select students by name. `student = classroom.get_student("Sarah")` now `student` is your `Person` object. That's why I chose a dictionary. If you want to access by numbers than maybe just consider using a list. Other users in this thread have examples of using a list. – ptan9o Jun 02 '20 at 21:48