2017-04-15 75 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

這是錯誤:列表索引超出範圍 –