-1

The problem I am currently having occurs when trying to use the collections sorted method to sort a dict made to keep the user's name and highest score. While I have already implemented the editable high score feature using 3 lists one holds float value. One holds float value+' %' and the other holds the name of the player. The sort method on the dict made from zip(name, float value+' %')

** Will output a score of 9.99% < 10% to the top of the leaderboard

** Will put a score of 100% near the bottom of the leaderboard same as 10%.

Current using the method

sorted_position = sorted(position_collection.items(), key=lambda kv: kv[1], reverse=True)





position_collection=dict(zip(name_list,score_list))
             #step 4: creation of a dictionary from the name_list and score_list and sorting it by value
             sorted_position = sorted(position_collection.items(), key=lambda kv: kv[1], reverse=True)
             #This is the part of the code found on stack overflow it orders the dictionary by the key value I do not fully understand this code but it is causing issues.
             # I got it from here  - https://stackoverflow.com/questions/613183/how-do-i-sort-a-dictionary-by-value
             for z in range(0,len(sorted_position)):
                 this_tuple=tuple(sorted_position[z])
                 pos = z+1
              # This takes the position from the amount of tuples returned. Notice the cast to tuple. What was returned was an array I think? containing all of the tuples
              # When tuple = 0 it is the Name 1 is the score
                 if z!=len(sorted_position)-1:
                    #step 5: taking the tuples name and score and adding them to final_line the finished product that will be outputed.
                     final_line = final_line + str(pos)+". "+str(this_tuple[0])+" : "+str(this_tuple[1])+"\n"
                 else:
                     final_line = final_line + str(pos)+". "+str(this_tuple[0])+" : "+str(this_tuple[1])

             result = basetext+final_line

While expected scoreboard should be by a score higher at the top to lower at the bottom the return looks like this

==LeaderBoard==
1. Orion Nelson : 81.395%
2. Zahara Mahoney : 7.698%
3. Lyndsey Sadler : 7.683%
4. Joe Man : 58.337%
5. Myah Chapman : 27.265%
6. Thelma Parra : 27.263%
7. Grady Amin : 22.231%
8. Elouise Stokes : 15.394%
9. Ivie Contreras : 15.392%
10. Saniiya Bolton : 15.378%
11. Farhan Gallagher : 13.336%
12. Harmony Christian : 12.503%
13. Winner Winner : 108.58%

If anyone has any clue what is going on my thought is it is only registering the first two numbers but do not know why.

Thanks,

Kapil
  • 817
  • 2
  • 13
  • 25

1 Answers1

0

Figured It out sorry for asking such a stupid question new to python

sorted(position_collection.items(), key=lambda kv: float(kv[1][:-1]), reverse=True)

In my defence, I tried this earlier but had another bug which made me think It was still broken.