所以我有這個代碼,你會接受一個調查問卷,它會給你一個滿分50分。結果是數據/結果列表如果您在一個元組中多次使用調查問卷,那麼調查問卷就會針對每個人都包含您的姓名進行評分。它應該根據您的姓名按照字母順序打印數據並從高分到低分打印。我已經通過在另一個程序中使用填充列表測試了這一點,我將其插入到我的主代碼中,在那裏使用.dat文件,並且在標題中看到錯誤。下面是代碼:如何解決:AttributeError:'int'對象沒有屬性'排序'
results = []
High_Scores = []
for x in results:
temp = []
y = i[1].sort(reverse=True)
for i in reversed(x):
temp.append(x)
test2.append(temp)
results.sort()
High_Scores.sort(reverse=True)
print(results)
print(High_Scores)
下面是它的工作在我把它放在我的主代碼前的代碼:
test = [[ "jeff", [48, 38] ], ["ted", [17, 26, 10] ], ["lol", [19]], ["lovely", [2]]]
test2 = []
for x in test:
temp = []
y = x[1].sort(reverse=True)
for x in reversed(x):
temp.append(x)
test2.append(temp)
test.sort()
test2.sort(reverse=True)
print(test)
print(test2)
我嘗試這樣做:
results = []
High_Scores = []
High_Scores = sorted(results, key = lambda tup: tup[1], reverse = True)
print (High_Scores)
for x in results:
temp = []
y = x.sort(reverse=True)
for i in reversed(x):
temp.append(i)
High_Scores.append(temp)
results.sort()
High_Scores.sort(reverse=True)
print(results)
print(High_Scores)
,並得到這個作爲錯誤:
y = x.sort(reverse=True)
AttributeError: 'tuple' object has no attribute 'sort'
我試着運行你的代碼,我得到了'NameError:name'results'is not defined'。請[mcve]。 – Kevin
對不起,我忘了在:/ – EDoggyDog
哪條線上發生錯誤? 'results.sort'或'test2.sort'? – Barmar