我目前正在尝试使用内置的Sorted函数对一个名为scores的class属性的列表进行排序。你知道吗
在这里,排序函数只处理一个列表,它正确地按升序排列分数。你知道吗
Scores = [1,5,19,0,900,81,9000]
print(sorted(Scores))
但是,当我尝试将此函数应用于具有class属性的列表时,会返回一个错误: AttributeError:“list”对象没有“Score”属性
这是我的密码:
print(sorted(RecentScores.Score))
任何帮助都将不胜感激。你知道吗
下面是我正在初始化的班级和最近的分数表
class TRecentScore():
def __init__(self):
self.Name = ''
self.Score = 0
RecentScores = [1,5,0]
在类定义中,必须首先将
__init__
中的名称和分数作为输入,如下所示:只有在类内指定名称和分数才有意义。 接下来,必须实例化类
RecentScores
的TRecentScore
实例,将名称和分数作为参数传递给初始化。具体操作如下:这将打印出排序后的分数。希望这有帮助!你知道吗
如果您总是希望对分数列表进行排序,可以对列表进行排序,使用“排序”创建新列表:
你没有使用你的类,实际上你只是在声明一个包含一些数字的列表,然后你试图调用这个列表对象的属性
Score
。你知道吗相关问题 更多 >
编程相关推荐