亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定

為什么這樣不行呢?求大神解答!~

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__


? ? def __cmp__(self, s):

? ? ? ? if self.score>s.score:

? ? ? ? ? ? return 1

? ? ? ? elif self.score<s.socre:

? ? ? ? ? ? return -1

? ? ? ? else:

? ? ? ? ? ? return 0

? ? ? ??


L = [Student('Tim', 99), Student('Bob', 88), Student('Alice', 99)]

print sorted(L)


正在回答

3 回答

你這里只比較了分數,沒有比較分數相同時的名字排序

你的完善版本應該是這樣

https://img1.sycdn.imooc.com//5ca17bfb0001243707450838.jpg

2 回復 有任何疑惑可以回復我~

if self.score < s.score:

? ? ? ? ? ? return 1

? ? ? ? elif self.score > s.score:

? ? ? ? ? ? return -1

? ? ? ? else:

? ? ? ? ? ? return cmp(self.name,s.name)


0 回復 有任何疑惑可以回復我~

學習了

0 回復 有任何疑惑可以回復我~

舉報

0/150
提交
取消
python進階
  • 參與學習       255563    人
  • 解答問題       3038    個

學習函數式、模塊和面向對象編程,掌握Python高級程序設計

進入課程

為什么這樣不行呢?求大神解答!~

我要回答 關注問題
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號