1

Here's a function I created for challenge 7.2 in Dawson book, but wanted to modify it a little bit. To not spam I'm sending just key place as other is not related to the problem.

def results(score):  
     name = input("What's your name? ")
     s = shelve.open("results.dat")
     s[str(score)] = [name]
     s.sync()
     scores = list(s.keys())
     scores.sort(reverse=True)
     print("Top 5 results in quiz game:\n")
     for i in scores[:5]:
         print("Points:", i, "Player:", "".join(s[i]))
     s.close()

My output looks:

Points: 8 player: Chris
Points: 6 player: klsjahd
Points: 5 player: dlahdlksjah
Points: 4 player: dnlsandlknasd
Points: 3 player: bleble

I'd want to add a row number, so I can show obvious thing like which top is it.

TOP 1: Points: 8 player: Chris
TOP 2: Points: 6 player: klsjahd
TOP 3: Points: 5 player: dlahdlksjah
TOP 4: Points: 4 player: dnlsandlknasd
TOP 5: Points: 3 player: bleble
Pranav Hosangadi
  • 23,755
  • 7
  • 44
  • 70
Mr. Chris
  • 35
  • 5
  • Please take a moment to read [formatting help](/help/formatting). You can format a block of code by putting three backticks (`) on the lines before and after your code. Since you're new here, please also take the [tour] and read [ask] and how to provide a [mre]. Welcome to Stack Overflow! – Pranav Hosangadi Mar 29 '22 at 01:22
  • Does this answer your question? [Accessing the index in 'for' loops?](https://stackoverflow.com/questions/522563/accessing-the-index-in-for-loops) – Pranav Hosangadi Mar 29 '22 at 01:25
  • Particularly [this answer](https://stackoverflow.com/a/28072982/843953). See the `start` argument of `enumerate()`. – Pranav Hosangadi Mar 29 '22 at 01:26
  • Yes, used new variable with value 1 then added += 1 after each loop, this fits perfectly for my current level. Thanks :) and sorry for a mess – Mr. Chris Mar 29 '22 at 01:38

0 Answers0