import shelve
import pickle
scoreFile = shelve.open('score.txt')
username = 'User1'
def updateScore(newScore):
if('score' in scoreFile):
score = scoreFile['score']
if(newScore not in score):
score.insert(0, newScore)
score.sort()
ranking = score.index(newScore)
ranking = len(score)-ranking
else:
score = [newScore]
ranking = 1
if len(score) >5:
del(score[0])
else:
pass
print(score)
print(ranking)
scoreFile['score'] = score
places = {'first':'user',
'second': 'user',
'third': 'user',
'fourth': 'user',
'fifth' : 'user'
}
if ranking ==1:
places['first']= username
elif ranking==2:
places['second'] = username
elif ranking ==3:
places['third'] = username
elif ranking == 4:
places['fourth'] = username
elif ranking ==5:
places['fifth'] = username
else:
print("not on leaderboard")
filename = "names.txt"
file = open(filename, 'wb')
pickle.dump(places, file)
return ranking
newScore = int(input("New HighScore: \n"))
updateScore(newScore)
Я пытался использовать рассол и полку, как вы можете видеть выше, но диктат не спасает, я уверен, что есть лучшие способы сделать это. Все, что мне нужно сделать, это связать очки победителя и место пользователя и имя пользователя в одном go на доске лидеров для топ-5. Если кто-то может помочь, большое спасибо.