Python Forum
Is there a way of improving this leaderboard system? - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: Is there a way of improving this leaderboard system? (/thread-16028.html)



Is there a way of improving this leaderboard system? - Zelpha - Feb-11-2019

So me and my friend came up with this as a leaderboard system and im wondering are there any improvements that can be made to it? especially in the sorting section?

#prototype leaderboards

import csv

score=input("whats ya score")
username=input("whats ya name")

with open ("protleader.csv", "a", newline='') as file:
    fields=['score', 'name']
    writer=csv.DictWriter(file, fieldnames=fields)
    writer.writerow({'score' : score, 'name' : username})

with open ("protleader.csv", "r") as file:
    sortlist=[]
    reader=csv.reader(file)
    for i in reader:
        sortlist.append(i)
for i in range(len(sortlist)):
    if i != 0:
        sortlist[i][0]=int(sortlist[i][int(0)])
        

print("")

print("Unsorted:")
for i in range(len(sortlist)):
    print(sortlist[i])


for i in range(555):
    for i in range(len(sortlist)-1):
        if i != 0:
            if sortlist[i][0] < sortlist[i+1][0]:
                change=sortlist[i]
                sortlist[i]=sortlist[i+1]
                sortlist[i+1]=change
                

print("")

print("Sorted and cut:")
for i in range(len(sortlist)-1):
    print(sortlist[i])



RE: Is there a way of improving this leaderboard system? - woooee - Feb-11-2019

Which i does if i != 0: refer to, the first or second for i in
for i in range(555):
    for i in range(len(sortlist)-1):
        if i != 0: 



RE: Is there a way of improving this leaderboard system? - ichabod801 - Feb-11-2019

As woooee implies, you are using the variable name i way to much. Use more descriptive variable names. Also, loop directly over lists, not their indices. Not like this:

print("Unsorted:")
for i in range(len(sortlist)):
    print(sortlist[i])
But like this:

print("Unsorted:")
for score in sortlist:
    print(sortlist)
sortlist is another bad name. Okay, it's a list you're going to sort. What is it a list of? That would make a better name.

You don't need to write your own sort, there is a very efficient one built in:

sortlist.sort(key row: row[0])