Reputation: 7
I know this is a simple error but been looking at it all day! Where do I add float or int to prevent the following error message? int object is not utterable.
How would I print it from highest to lowest score. Can I add reverse=True? I'm getting a tuple error. –
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])))
Upvotes: 0
Views: 1195
Reputation: 82899
The problem is this line:
print("{}'s best score is {}".format(name, max(scores[name])))
here, you are trying to take the max
of scores[name]
, which is just a single integer. Looking at the code, it seems like you already took care of that value being the maximum value, so you can just change that line to
print("{}'s best score is {}".format(name, scores[name]))
as in the print
statement above. (Also, since those two print
lines will print the same thing, you can probably remove one of those two.)
To print it from highest to lowest score, change your for
loop to something like this:
for name in sorted(scores, key=scores.get, reverse=True):
...
This sorts the names in scores
using the scores.get
function as key, i.e. it sorts by the values in the dictionary, and the reverse=True
makes it sort from highest to lowest.
Upvotes: 1