#π help creating a leaderboard in game
22 messages Β· Page 1 of 1 (latest)
@left anvil
Remember to:
- Ask your Python question, not if you can ask or if there's an expert who can help.
- Show a code sample as text (rather than a screenshot) and the error message, if you've got one.
- Explain what you expect to happen and what actually happens.
:warning: Do not pip install anything that isn't related to your question, especially if asked to over DMs.
oh dear
import random
import re
def mode_select():
while True:
mode = input("pick a mode: easy, normal, hard").strip().lower()
if mode == "easy":
print(f"you have selected {mode}, each time you get a number correct, it will tell you which position is right\n")
break
elif mode == "normal":
print(f"you have selected {mode}, when you get a nuumber in the correct position, it will tell you how many numbers are in the right position\n")
break
elif mode == "hard":
print (f"you have selected {mode}, the code is now 5 numbers long instead of the usual 4.\n")
break
else:
print (f"please select a mode from: easy, normal or hard")
return mode
def digits(mode):
if mode == "easy":
digitsList = [random.randint(0,9) for _ in range(4)]
elif mode == "normal":
digitsList = [random.randint(0,9) for _ in range(4)]
elif mode == "hard":
digitsList = [random.randint(0,9) for _ in range (5)]
return digitsList
def guessing(mode, digitsList):
counter = 1
guessNumberList = []
if mode == "easy":
while True:
print (f"\nattempt {counter}")
guessNumber = input("\ngang, give me a four digit number")
if not re.fullmatch(r'\d{4}', guessNumber):
print ("enter a four digit number")
continue
guessNumberList = [int(x) for x in guessNumber]
correct_digit_placement = [num + 1 for num in range(len(digitsList)) if digitsList[num] == guessNumberList[num]]
correct_digits = sum(1 for g, d in zip(guessNumberList, digitsList) if g == d)
print (f"you got numbers right at positions {correct_digit_placement}")
print (f"you guessed {correct_digits} places right")
counter += 1
if guessNumberList == digitsList:
print ("well done bro ggs")
return
guessNumberList.clear()
elif mode == "normal":
while True:
print (f"\nattempt {counter}")
guessNumber = input("\ngang, give me a four digit number")
if not re.fullmatch (r'\d{4}', guessNumber):
print ("enter a four digit number")
continue
guessNumberList = [int(x) for x in guessNumber]
correct_digits = sum(1 for g, d in zip(guessNumberList, digitsList) if g == d)
print (f"you guessed {correct_digits} places right")
counter += 1
if guessNumberList == digitsList:
print ("well done bro ggs")
return
guessNumberList.clear()
elif mode == "hard":
while True:
print (f"\nattempt {counter}")
guessNumber = input("\ngang, enter a five digit number")
if not re.fullmatch (r'\d{5}', guessNumber):
print ("enter a five digit number")
continue
guessNumberList = [int(x) for x in guessNumber]
correct_digits = sum(1 for g, d in zip(guessNumberList, digitsList) if g == d)
print (f"you guessed {correct_digits} places right")
counter += 1
if guessNumberList == digitsList:
print ("well done bro ggs")
return
guessNumberList.clear()
return counter
def main():
mode = mode_select()
digitsList = digits(mode)
guessing(mode, digitsList)
if __name__ == "__main__":
main()
#create a leaderboard for it
ive been trying to create a leaderboard in game, that is displayed after each run of the game, which is just {name:score}, but im not sure how to open a file and create a dictionary within it and then also sort it and print it at the end of the code.
it would be appreciated if you also see any easier ways i could do my code as i started learning a week ish ago and just explain what you did if possible!
!e
import shelve
# your game
# store data
with shelve.open("scores") as scores:
scores["player1"] = 5
scores["player2"] = 12
# load data
with shelve.open("scores") as scores:
print(scores["player1"])
print(scores["player2"])
:white_check_mark: Your 3.12 eval job has completed with return code 0.
001 | 5
002 | 12
Files with no extension can't be uploaded.
and here how you can sort you data:
!e
import shelve
import random
# your game
# store data
with shelve.open("scores") as scores:
for i, j in zip(range(20), (random.randint(0, 100) for _ in range(20))):
scores[f"player{i}"] = j
# load data
with shelve.open("scores") as scores:
leaderboard = sorted(scores.items(), key=lambda x: -x[1])
for player, score in leaderboard:
print(f"{player}: {score}")
:white_check_mark: Your 3.12 eval job has completed with return code 0.
001 | player19: 96
002 | player6: 83
003 | player4: 76
004 | player13: 76
005 | player9: 74
006 | player10: 71
007 | player18: 68
008 | player5: 66
009 | player16: 57
010 | player0: 51
... (truncated - too many lines)
Full output: https://paste.pythondiscord.com/MXPTZOAL4JCGJW3RIUQOEFCFAU
Files with no extension can't be uploaded.
@left anvil
so should i have scores, be attempts and for player, make an input for a name?
if that's what you want
is it possible so it saves to an existing file, so the leaderboard saves even when the program is clsoed?
that is what shelve does
look at the documentation link i shared above
This help channel has been closed and it's no longer possible to send messages here. If your question wasn't answered, feel free to create a new post in #1035199133436354600. To maximize your chances of getting a response, check out this guide on asking good questions.