我知道這是一個簡單的錯誤,但一直在看它!我在哪裏添加float或int以防止出現以下錯誤消息? int對象不是可用的。使用字典時int對象不可迭代
如何從最高分到最低分打印它。我可以添加reverse = True嗎?我收到一個元組錯誤。 -
scores = {} #You'll need to use a dictionary to store your scores;
with open("classscores1.txt") as f:
for line in f:
name, score = line.split() #store the name and score separately (with scores converted to an integer)
score = int(score)
if name not in scores or scores[name] < score:
scores[name] = score # replacing the score only if it is higher:
for name in sorted(scores):
print(name, "best score is", scores[name])
print("{}'s best score is {}".format(name, max(scores[name])))
'int'對象在任何地方都不可迭代*。 – jonrsharpe
'max(scores [name])'在這裏,'scores [name]'只是一個單獨的分數,一個'int'。這應該是該名稱的分數列表嗎? –
最後一行,大概是引發錯誤的那一行,看起來應該和以前的行完全一樣。所以你可能應該刪除那個有問題的行。 –