Why we should write object inside or outside the loop?


#1

class student(object):
    
    def student(self):
        self.name=input("enter name:")
        self.stno=int(input("enter stno:"))
        self.score=int(input("enter score:"))
    def dis(self):
        print("name:",self.name,"stno:",self.stno,"score:",self.score)
    def stno(self):
        return self.stno
    def name(self):
        return self.name
    def score(self):
        return self.score



y=[]
while True:
    c=int(input("enter 1-2))

    if c==1:
        a=student()
        a.student()
        y.append(a)
            
    elif c==2:
        for st in y:
            st.dis()

I defined above code via using one array.Then I changed that and I want to use 3 arrays for name,student number and score as below:
class student(object):

        def __init__(self):
            self.c=0
            self.x=[]
            self.y=[]
            self.z=[]
        def vrod(self):
            self.x.append(input("enter name:"))
            self.z.append(int(input("enter #:")))
            self.y.append(int(input("enter score:")))
            self.c+=1
        def report(self):
            for self.i in range(len(self.x)):
                print("name:",self.x[self.i],"st number:",self.z[self.i],"score:",self.y[self.i])


a=student()
while True:
      o=int(input("enter menu:"))
    if o==1:
       a.vrod() 
    elif o==2:
        a.report()

Why should we use a=student() inside the loop in the first one and outside in the second one? If not, it will go wrong. I cannot understand.
Meanwhile, Which of them is common in Python. I mean writing one array or several arrays for working with Python in expert point of view?


#2

oum, what is you want, this is not a lesson in codecademy, right?

if you want to save your student data, you must create an object from class student.
in this class should be save name and personal details.(this is need your mind for how to design class student, get score or no and...)
now you have an student with some details that you want.
now you create a another student with another details.

now in end step, you want to access this student. you can use an array to put this student object into that.

then you must call a cell of array to access to him, or use an dictionary to get these better. this is a solution


#3

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.