Reputation: 33
class student:
def __init__(self, name, age, mark):
self.name = name
self.age = age
self.mark = mark
std1 = student("name1", 14, 45)
std2 = student("name2", 13, 90)
std3 = student("name3", 14, 70)
std4 = student("name4", 14, 80)
std5 = student("name5", 13, 75)
listofStds = ["std1", "std2", "std3", "std4", "std5"]
for x in sorted(listofStds,key=lambda x: x.mark):
print x
Please help. I'm a python beginner and I am trying out this simple sorting program using classes but I keep getting the following errors
AttributeError: 'str' object has no attribute 'mark'
Upvotes: 1
Views: 72
Reputation: 14581
listofStds
is a list of strings, not a list of student
instances. You are trying to sort it by attribute mark
which doesn't exist for strings.
You probably wanted to have a list of students
, so you need this:
listofStds = [std1, std2, std3, std4, std5]
Upvotes: 5