I have a class name Student like this:
class Student:
def __init__(self, name = '', age = 0, test_score = 0):
self.name = name
self.age = age
self.test_Scores = test_score
def __str__(self):
return "({0},{1},{2})".format(self.name,self.age, self.test_Scores)
and class name Students:
class Students():
stds = list()
def __init__(self) -> None:
pass
def Input(self):
while True:
inputName = input('Enter name: ')
inputAge = int(input('Enter age: '))
inputTestScore = int(input('Enter test score: '))
std = Student(inputName, inputAge, inputTestScore)
if inputAge == 0:
break
self.stds.append(std)
def __str__(self):
return str(self.stds)
Here are some code print out a list of students:
stds = Students()
stds.Input()
print(stds)
With 2 elements in the list, the result after excute look like this: [<main.Student object at 0x0000026EDEBC5FA0>, <main.Student object at 0x0000026EDEBC5CD0>]
I can't print out stds
under a string, how can i fix it. And how to sort stds
by the decreasing of age ?
Pls help me !
CodePudding user response:
You shouldn't have class Students
, you should have a list of Student
. To get the data from the class variables override the __repr__
method. To sort the list you can use lambda
with the attribute to sort by as key in sort()
function
class Student:
def __init__(self, name='', age=0, test_score=0):
self.name = name
self.age = age
self.test_Scores = test_score
def __repr__(self):
return "({0},{1},{2})".format(self.name, self.age, self.test_Scores)
if __name__ == '__main__':
stds = list()
while True:
inputName = input('Enter name: ')
inputAge = int(input('Enter age: '))
inputTestScore = int(input('Enter test score: '))
std = Student(inputName, inputAge, inputTestScore)
if inputAge == 0:
break
stds.append(std)
stds.sort(key=lambda s: s.name)
print(stds)
CodePudding user response:
You need to write the function str for students class.
def __str__(self):
return '\n'.join(self.stds)
CodePudding user response:
try this =>
class Student:
def __init__(self, name = '', age = 0, test_score = 0):
self.name = name
self.age = age
self.test_Scores = test_score
def __str__(self):
return "({0},{1},{2})".format(self.name,self.age, self.test_Scores)
def __repr__(self):
return "({0},{1},{2})".format(self.name,self.age, self.test_Scores)
def __lt__(self, other):
return self.age < other.age
class Students():
stds = list()
def __init__(self) -> None:
pass
def Input(self):
while True:
inputName = input('Enter name: ')
inputAge = int(input('Enter age: '))
inputTestScore = int(input('Enter test score: '))
std = Student(inputName, inputAge, inputTestScore)
if inputAge == 0:
break
self.stds.append(std)
def __str__(self):
return str(self.stds)
stds = Students()
stds.Input()
print(stds)
stds.stds.sort(reverse=True)
print(stds)
CodePudding user response:
Define a natural order for the students based on their age using @functools.total_ordering
:
import functools
@functools.total_ordering
class Student:
def __init__(self, name='', age=0, test_score=0):
self.name = name
self.age = age
self.test_Scores = test_score
def __lt__(self, other):
return self.age < other.age
def __str__(self):
return "({0},{1},{2})".format(self.name, self.age, self.test_Scores)
def test_sorting_students():
alice: Student = Student("Alice", 21, 86)
bob: Student = Student("Bob", 19, 75)
caroline: Student = Student("Caroline", 20, 93)
students = [alice, bob, caroline]
students.sort()
assert students[0] == bob
assert students[1] == caroline
assert students[2] == alice
Then just sort them:
print(stds.sort())