Bottom Page

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])
Quote
#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: 
Quote
#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

Quote

Top Page

Possibly Related Threads...
Thread Author Replies Views Last Post
  Get system info from PI (cpu load and temp) korenron 2 131 Aug-04-2019, 08:45 AM
Last Post: korenron
  FileNotFoundError...System cannot find the path specified alex9745 28 3,606 Jul-23-2019, 05:14 AM
Last Post: alex9745
  Difference between os.system("clear") and os.system("cls") chmsrohit 6 475 Jun-16-2019, 11:38 AM
Last Post: DeaD_EyE
  Improving bot SheeppOSU 0 229 Jun-01-2019, 08:06 PM
Last Post: SheeppOSU
  Can I make a session on os.system Serferi 1 185 May-09-2019, 06:20 PM
Last Post: woooee
  Opening Directories on anothe File System mickrobinson567812 2 274 Apr-25-2019, 04:59 AM
Last Post: mickrobinson567812
  System of 3 non-linear equations in 3 unknowns (how-to-solve?) samsonite 2 262 Mar-23-2019, 10:14 AM
Last Post: samsonite
  Cannot add dot executable to system path bobsmith76 2 427 Mar-05-2019, 03:17 PM
Last Post: bobsmith76
  Login System Carbonix 4 598 Feb-04-2019, 02:19 PM
Last Post: Larz60+
  [python]FileNotFoundError...System cannot find the path specified creichle 1 703 Jan-30-2019, 12:49 AM
Last Post: Larz60+

Forum Jump:


Users browsing this thread: 1 Guest(s)