Python Forum

Full Version: Is there a way of improving this leaderboard system?
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
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])
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: 
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])