Python Forum
Is there a way of improving this leaderboard system?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Is there a way of improving this leaderboard system?
#1
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])
Reply
#2
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: 
Reply
#3
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])
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Difference between os.system("clear") and os.system("cls") chmsrohit 7 16,587 Jan-11-2021, 06:30 PM
Last Post: ykumar34
  Need help improving function that reads file into list of tuples t4keheart 6 3,031 Nov-03-2020, 04:58 AM
Last Post: ndc85430
  Improving my understanding of functions and methods menator01 2 2,126 Apr-24-2020, 06:26 AM
Last Post: menator01
Question Difference between Python's os.system and Perl's system command Agile741 13 6,769 Dec-02-2019, 04:41 PM
Last Post: Agile741
  Improving code to autorename if filename exists Den0st 5 2,990 Sep-23-2019, 07:53 AM
Last Post: wavic
  Improving bot SheeppOSU 0 21,954 Jun-01-2019, 08:06 PM
Last Post: SheeppOSU
  Improving Efficiency of SVM by various available kernels Sachtech 0 2,077 Apr-09-2018, 07:29 AM
Last Post: Sachtech
  Question on runtime and improving nested for loops ackmondual 1 3,037 Jun-13-2017, 11:11 PM
Last Post: ichabod801

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020