为啥我的代码结果不对,是[(Tim: 99), (Bob: 88), (Alice: 99)]
为啥我的代码结果不对,是[(Tim: 99), (Bob: 88), (Alice: 99)]
2016-02-28
你的代码缩进有问题
class Student(object): def __init__(self, name, score): self.name = name self.score = score def __str__(self): return '(%s: %s)' % (self.name, self.score) __repr__ = __str__ #question def __cmp__(self, s): if self.score < s.score: return 1 elif self.score > s.score: return -1 elif self.score == s.score: if self.name < s.name: return -1 elif self.name > s.name: return 1 return 0 L = [Student('Tim', 99), Student('Bob', 88), Student('Alice', 99)] print sorted(L)
举报