Ask coding questions

← Back to all posts
Money leader board
h
Kingq23 (38)

Using replit database can i make a leader board?

Answered by OldWizard209 (1543) [earned 5 cycles]
View Answer
Comments
hotnewtop
OldWizard209 (1543)

It is pretty simple to make:

First, create an array of scores. Append the new scores if you need to:

list_of_scores = [41, 98, 12, 44, 76, 89, 77]

Then sort the list using .sort() and set reverse as a parameter to True:

list_of_scores = [41, 98, 12, 44, 76, 89, 77]
list_of_scores.sort(reverse=True)

Then import the replit database and set the list to a key:

from replit import db

list_of_scores = [41, 98, 12, 44, 76, 89, 77]
list_of_scores.sort(reverse=True)
db["leader_board"] = list_of_scores 

Then to format the leader board, use a simple for loop:

from replit import db


list_of_scores = [41, 98, 12, 44, 76, 89, 77]
list_of_scores.sort(reverse=True)
db["leader_board"] = list_of_scores

for i in range(len(list_of_scores)):
    print(f"Position {i+1} : {list_of_scores[i]}")

Output:

Position 1 : 98
Position 2 : 89
Position 3 : 77
Position 4 : 76
Position 5 : 44
Position 6 : 41
Position 7 : 12

If you want to keep adding scores, then you can create a variable, set it to a db and keep appending to it and use the same code:

from replit import db

list_of_scores = list(db["leader_board"]) 
list_of_scores.sort(reverse=True)


for i in range(len(list_of_scores)):
    print(f"Position {i+1} : {list_of_scores[i]}")

If you want to know more about the replit database, Click the database icon on the left panel on your repl.
Hope I helped !!!!!

:P

Kingq23 (38)

@OldWizard209 wait acually how would i tie a username to this

OldWizard209 (1543)

Can you explain more clearly what you are trying to do? @qadams2008

Kingq23 (38)

@OldWizard209 make a leader board of who has the most money in the database.does that help

OldWizard209 (1543)

OHHH. that is verrryyy easy. You just need to use a dictionary, change it to a list, append any userss you want. Da code:

from replit import db


data = {
        "user1" : 1000,
        "user2" : 1342,
        "user3" : 1938,
        "user4" : 1093,
        "user5" : 890
    }

user_list = list(data.keys())
money_list = list(data.values())

money_list.sort(reverse=True)

# db["leader_board"] = money_list

for i in money_list:
    print(f"{user_list[money_list.index(i)]} : ${i}")

@qadams2008

Coder100 (18209)

you could store an array of players, and then you sort it so you get a leaderboard!

Kingq23 (38)

@Coder100 hello? how would i do that.