2017-04-15 27 views
-3

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

这是我此刻的代码,但它是在改善严重的需要:

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) 

正如你所看到的代码量还没有存储任何。请帮忙。

+0

您是否尝试过做的文件在Python处理一些研究? – jonrsharpe

+0

我对这个问题仍然陌生,因为你可能能够说出并尝试过研究,但在我的问题上找不到任何东西,所以我在这里问了一下,希望人们能够教我一些东西。 –

+1

这不是教程网站,您的问题在这里不合适。有许多关于文件处理的在线教程,包括[官方教程](https://docs.python.org/3/tutorial/inputoutput.html#reading-and-writing-files)。还有一些书籍:[Python Crash Course](https://www.nostarch.com/pythoncrashcourse/)有一节(第14章),用于节省游戏的高分。 –

回答

0

要创建本地文件,您可以使用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() 

您可以使用相同的程序来存储其他值。 要检索就可以使用,

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] 
+0

更新:在这段代码中有错误, –

+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] ndhighscorename = data [1] .split()[1] .split(' - ')[0] ndhighscore = int(data [1] .split()[1] .split(' - ')[1] ] rdhighscorename = data [2] .split()[1] .split(' - ')[0] [0] [0] rdhighscore = int(data [2] .split()[1] .split(' - ' 0] –

+0

这是错误:列表索引超出范围 –