Python 我试图在掷骰子游戏中实现一个领头板系统,但我所有的尝试都失败了,有没有办法做到这一点?
我试着用pickle和shelve,正如你在上面看到的,但是dict不能保存,我相信有更好的方法来实现这一点。我所需要做的就是将获胜者的分数、用户的位置和用户的姓名一次性关联到前5名的排行榜上。如果有人能帮忙,非常感谢。我不知道shelve或pickle,但你似乎只是在打开文件。在任何情况下,您都不会保存更改的数据。既然数据看起来只是一个简单的dict,为什么不使用json?我目前对json不太了解,我认为如果我对它进行一些研究,这是可能的。有没有更简单的方法,比如csv(我也没学过)我不知道shelve或pickle,但看起来你只是在打开文件。在任何情况下,您都不会保存更改的数据。既然数据看起来只是一个简单的dict,为什么不使用json?我目前对json不太了解,我认为如果我对它进行一些研究,这是可能的。有没有更简单的方法,比如csv(我也没学过这个)Python 我试图在掷骰子游戏中实现一个领头板系统,但我所有的尝试都失败了,有没有办法做到这一点?,python,pickle,leaderboard,shelve,Python,Pickle,Leaderboard,Shelve,我试着用pickle和shelve,正如你在上面看到的,但是dict不能保存,我相信有更好的方法来实现这一点。我所需要做的就是将获胜者的分数、用户的位置和用户的姓名一次性关联到前5名的排行榜上。如果有人能帮忙,非常感谢。我不知道shelve或pickle,但你似乎只是在打开文件。在任何情况下,您都不会保存更改的数据。既然数据看起来只是一个简单的dict,为什么不使用json?我目前对json不太了解,我认为如果我对它进行一些研究,这是可能的。有没有更简单的方法,比如csv(我也没学过)我不知道s
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)