I am trying to create a high score system for my game, but only want to display the top 5 high scores. I used a dictionary to store the scores and the names of the players. I want the program to remove the first score once there are more than 5 items. How do I remove items from a dictionary based on their order?
I tried to use .pop(index)
like so:
highscores = {"player1":"54", "player2":"56", "player3":"63", "player4":"72", "player5":"81", "player6":"94"}
if len(highscores) > 5:
highscores.pop(0)
However I get an error:
Traceback (most recent call last):
File "c:\Users\-----\Documents\Python projects\Python NEA coursework\test.py", line 3, in <module>
highscores.pop(0)
KeyError: 0
Anyone know why this happens?
I found a solution:
highscores = {"player1":"54", "player2":"56", "player3":"63", "player4":"72", "player5":"81", "player6":"94"}
thislist = []
for keys in highscores.items():
thislist += keys
highscores.pop(thislist[0])