[英]Attribute Error While Trying to Run Insertion Sort
I am currently creating a turtle game which will collect scores and initials in an array of records, I want to run an insertion sort in order to display to the user if they had received one of the top three scores.我目前正在创建一个乌龟游戏,它将在一系列记录中收集分数和首字母,我想运行插入排序以向用户显示他们是否收到了前三名之一。 However, whenever I try to run the code I receive an attribute error.
但是,每当我尝试运行代码时,我都会收到一个属性错误。 Code here (Can supply more code if required):
代码在这里(如果需要可以提供更多代码):
def insertion_sort(scores):
value = 0
i=0
for i in range(1, len(scores)):
value = scores[i].Score
j = i - 1
while j>= 0 and scores[j].Score > value:
scores[j + 1].Score = scores[j].Score
j -=1
scores[j + 1].Score = value
return scores
I have tried rearranging my data and changing how it has been stored however nothing has helped.我曾尝试重新排列我的数据并更改数据的存储方式,但没有任何帮助。 I'm unsure of what else to try as I have tried to work out several different solutions without any success.
我不确定还有什么可以尝试,因为我已经尝试过几种不同的解决方案,但都没有成功。
Is it possible you were just passing a list of numbers to the fuction?您是否有可能只是将数字列表传递给函数? Because that could also explain the attribute error:
AttributeError: 'int' object has no attribute 'Score'
因为这也可以解释属性错误:
AttributeError: 'int' object has no attribute 'Score'
You can easily convert a list of numbers to a list of records using a list comprehension.您可以使用列表理解轻松地将数字列表转换为记录列表。 (Or a dictionary like in the first example)
(或者像第一个例子中的字典)
Otherwise your record is likely missing the Score attribute (did you use a capital for it too?)否则您的记录可能缺少 Score 属性(您是否也使用了大写字母?)
I've also optimised the code a bit using an enumerate and slice, and switching the records instead of the values.我还使用枚举和切片对代码进行了一些优化,并切换记录而不是值。
they can be created from a dictionary of names with scores:它们可以从带有分数的名称字典中创建:
class Record:
def __init__(self, Name, Score):
self.Name = Name
self.Score = Score
def __repr__(self):
return "%s:%d" % (self.Name, self.Score)
def make_scores(scores):
return [Record(Name, Score) for Name, Score in scores.items()]
def insertion_sort(scores):
for j, score in enumerate(scores[1:]):
value = score.Score
while j >= 0 and scores[j].Score > value:
scores[j + 1] = scores[j]
j -= 1
scores[j + 1] = score
return scores
scores = make_scores({
"Alpha": 0,
"Bravo": 1,
"Charlie": 2,
"Delta": 3,
"Echo": 7
})
print(insertion_sort(scores))
scores = make_scores({
"Alpha": 7,
"Bravo": 3,
"Charlie": 2,
"Delta": 1,
"Echo": 0
})
print(insertion_sort(scores))
Output: Output:
[Alpha:0, Bravo:1, Charlie:2, Delta:3, Echo:7]
[Echo:0, Delta:1, Charlie:2, Bravo:3, Alpha:7]
I hope this is helful.我希望这是有帮助的。 Explanation:
解释:
__init__
function of Record just stores a name and a score. __init__
function 只是存储了一个名字和一个分数。__repr__
function of Record was just added in order to be able to visualise the record, you don't really need it __repr__
function 只是为了能够可视化记录而添加的,你并不是真的需要它they can be created from a list of numbers:它们可以从数字列表中创建:
class Record:
def __init__(self, score):
self.Score = score
def __repr__(self):
return "%d" % self.Score
def make_scores(scores):
return [Record(score) for score in scores]
def insertion_sort(scores):
for j, score in enumerate(scores[1:]):
value = score.Score
while j >= 0 and scores[j].Score > value:
scores[j + 1] = scores[j]
j -= 1
scores[j + 1] = score
return scores
scores = make_scores([0,1,2,3,7])
print(insertion_sort(scores))
scores = make_scores([7,3,2,1,0])
print(insertion_sort(scores))
Output: Output:
[0, 1, 2, 3, 7]
[0, 1, 2, 3, 7]
Explanation:解释:
__init__
function of Record just stores a score. __init__
function 只是存储一个分数。__repr__
function of Record was just added in order to be able to visualise the record, you don't really need it __repr__
function 只是为了能够可视化记录而添加的,你并不是真的需要它
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.