如何创建本地高分python

如何创建本地高分python,python,Python,我目前正在编写一段代码,其中用户根据他们在游戏中的表现设置分数,我被卡住了。是否有人可以就如何在用户计算机上创建本地文件并检索该文件提供一些建议,以便配置新的高分(如果有),然后打印最高的三个并删除其他文件以节省空间 这是我目前拥有的代码,但它急需改进: print("Yore score is", score, "/30") if sthighscore < score: sthighscore = score sthighscorename = name pr

我目前正在编写一段代码,其中用户根据他们在游戏中的表现设置分数,我被卡住了。是否有人可以就如何在用户计算机上创建本地文件并检索该文件提供一些建议,以便配置新的高分(如果有),然后打印最高的三个并删除其他文件以节省空间

这是我目前拥有的代码,但它急需改进:

print("Yore score is", score, "/30")

if sthighscore < score:
    sthighscore = score
    sthighscorename = name
    print("NEW 1st HIGH SCORE: ")
    print('1: ',sthighscorename, '-', sthighscore)
    print("2: ", ndhighscorename, '-', ndhighscore)
    print('3: ', rdhighscorename, '-', rdhighscore)

elif sthighscore >= score > ndhighscore:
    ndhighscore = score
    ndhighscorename = name
    print("NEW 2nd HIGH SCORE: ")
    print('1: ', sthighscorename, '-', sthighscore)
    print("2: ", ndhighscorename, '-', ndhighscore)
    print('3: ', rdhighscorename, '-', rdhighscore)

elif ndhighscore >= score > rdhighscore:
    rdhighscore = score
    rdhighscorename = name
    print("NEW 3rd HIGH SCORE: ")
    print('1: ',sthighscorename, '-', sthighscore)
    print("2: ", ndhighscorename, '-', ndhighscore)
    print('3: ', rdhighscorename, '-', rdhighscore)

else:
    print("NO NEW HIGH SCORES :( ")
    print('1: ',sthighscorename, '-', sthighscore)
    print("2: ", ndhighscorename, '-', ndhighscore)
    print('3: ', rdhighscorename, '-', rdhighscore)
打印(“以前的分数是”,分数,/30”)
如果sthighscore<分数:
sthighscore=分数
sthighscorename=名称
打印(“新的第一高分:”)
打印('1:',sthighscorename',-',sthighscore)
打印(“2:,ndhighscorename,”-,ndhighscore)
打印('3:',rdhighscorename',-',rdhighscore)
elif sthighscore>=分数>ndhighscore:
ndhighscore=分数
ndhighscorename=名称
打印(“新的第二高分:”)
打印('1:',sthighscorename',-',sthighscore)
打印(“2:,ndhighscorename,”-,ndhighscore)
打印('3:',rdhighscorename',-',rdhighscore)
elif ndhighscore>=分数>rdhighscore:
rdhighscore=分数
rdhighscorename=名称
打印(“新的第三高分:”)
打印('1:',sthighscorename',-',sthighscore)
打印(“2:,ndhighscorename,”-,ndhighscore)
打印('3:',rdhighscorename',-',rdhighscore)
其他:
打印(“无新高分:(”)
打印('1:',sthighscorename',-',sthighscore)
打印(“2:,ndhighscorename,”-,ndhighscore)
打印('3:',rdhighscorename',-',rdhighscore)

正如您所见,代码尚未存储任何内容。请提供帮助。

要创建本地文件,可以使用open()函数。您可以这样存储分数

print("Yore score is", score, "/30")
if sthighscore < score:
    sthighscore = score
    sthighscorename = name
    print("NEW 1st HIGH SCORE: ")
    print('1: ',sthighscorename, '-', sthighscore)
    print("2: ", ndhighscorename, '-', ndhighscore)
    print('3: ', rdhighscorename, '-', rdhighscore)
    f=open('score.txt','w') # This will create score.txt text file 
    f.write('%s\n%s\n%s'%('1: '+sthighscorename+ '-'+ str(sthighscore),"2: "+ ndhighscorename+ '-'+ str(ndhighscore), '3: '+ rdhighscorename+ '-'+ str(rdhighscore)))
    f.close()

你有没有试过用Python做一些关于文件处理的研究?我还是个新手,因为你可能会告诉我,也尝试过研究,但在我的问题上找不到任何东西,所以我在这里问它,希望人们能教我一些东西。这不是一个教程网站,你的问题不适合这里。这里有很多在线教程关于文件处理,包括。还有一些书:有一节(第14章)介绍如何为游戏保存高分。f=open('score.txt','r')data=f.read().split('\n')print(data)sthighscore=int(data[0].split()[1].split('-')[1])sthighscorename=data[0].split()[1].split split('-')[0]ndhighscore=int(data[1].split split(“-”)[1])ndhighscorename=data[1]。split()[1]。split(“-”)[0]rdhighscore=int(data[2]。split()[1]。split(“-”)[1])rdhighscorename=data[2]。split()[1]。split(“-”)[0]
f=open('score.txt','r')
data=f.read().split('\n')
print data
sthighscore=int(data[0].split()[1].split('-')[1])
sthighscorename=data[0].split()[1].split('-')[0]
ndhighscore=int(data[1].split()[1].split('-')[1])
ndhighscorename=data[1].split()[1].split('-')[0]
rdhighscore=int(data[2].split()[1].split('-')[1])
rdhighscorename=data[2].split()[1].split('-')[0]