0

I am making a game in python. At the beginning, the User enters his name and when he finishes the game, the Programm is saving the name and the playing time in a text file, like this:

Playerx: 12:04
Playery: 4:54
Playerz: 7:12

The program converts the text file to dictionary, like this:

scorelist = {'Playerx': '12:04', 'Playery': '4:54', 'Playerz': '7:12'}

Now I am searching for a possibility to sort the elements and print the 3 lowest at the end (highscore). Can you help me?

Einstein
  • 3
  • 1
  • Are those values time of day or runtime (ex: 12 minutes 4 seconds)? Also, shouldn't they be strings in the dcictionary ('PlayerX': '12:04')? – 001 Feb 07 '21 at 15:11
  • Yes, the values are for minutes and seconds – Einstein Feb 08 '21 at 14:38
  • I would store the times as seconds: `{'Playerx': 724, 'Playery': 294, 'Playerz': 432}` Then sort like in the duplicate. – 001 Feb 08 '21 at 14:54
  • Yea, that is a good idea, but could you explain how I can do this? – Einstein Feb 08 '21 at 16:34
  • You can convert the strings to integers with: `s = "12:04"; t = int(s.split(':')[0]) * 60 + int(s.split(':')[1])`. And convert the int values to strings with: `t = 724; f"{t // 60}:{t % 60:02}"` – 001 Feb 08 '21 at 17:18

0 Answers0