Python Dictionary Questions

Asked 2 years ago, Updated 2 years ago, 130 views

Hi, everyone. I have a question regarding Python dictionary function.

We created a function that matches the following largest score - person names. I think it's recognized as 2,1 if the score is 20,19. Is there something wrong with the code?

The text file is shown below.

dictionary python hash

2022-09-22 18:32

1 Answers

It's because the score is a string.

scores={}

result_f=open("./result.txt")

for line in result_f:
    (name,score)=line.split()
    print(type(score)) # <class 'str'>
    score = int(score)
    scores[score]=name
result_f.close()

print(scores)

for each_score in sorted(scores.keys(), reverse=True):
    r = "Surfer: {0}, Scored: {1}".format(scores[each_score], each_score)
    print(r)


2022-09-22 18:32

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.