sam98 sam98 - 4 months ago 7
Python Question

how can I use remove for this code?

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=[]
j=0
while(j<3):
a=student()
a.student()
y.append(a)
j+=1


for st in y:
st.dis()

n=int(input("enter #:"))
for c in y:
if c.stno==n:
y.remove(c.stno)


for st in y:
st.dis()


I want to remove one of the students with his/her student number and score via student number. I write:

n=int(input("enter #:"))
for c in y:
if c.stno==n:
y.remove(c.stno)


But obviously there is a problem with that. Do you know have can I modify that to use remove or del or pop, although I think pop or del cannot be workable as there is no index?

Answer

just call

y.remove(c)

when you do c.stno it won't exist in the list because what is actually in the list is the object itself not it's attributes