2017-02-21 5 views
1

나는 파이썬과 파이 게임을 사용하여 게임을 만들었고 시간과 이름을 저장하는 일을하려고 노력했습니다. 그러나 목록에 2 개의 항목이있는 경우 첫 번째 항목은 저장되고 정상적으로 작동하지만 게임을 완료 할 때마다 초 항목이 덮어 쓰기됩니다.내 2-d리스트가 최대 2 개의 아이템을 가지고있는 이유

try: 
    openFile = open("times.txt", "rb") 
    runTimes = pickle.load(openFile) 
    runTimes.append([g.name, g.count]) 
    openFile.close() 
except FileNotFoundError: 
    runTimes = [] 
    runTimes.append([g.name, g.count]) 
    openFile = open("times.txt", "wb") 
    pickle.dump(runTimes, openFile) 
    openFile.close() 

if len(runTimes) > 1: 
    print(runTimes) 

실행 1 = 아무것도 try: 블록 업데이트 성공도 때

실행이

[['Undefined', 7.5], ['Undefined', 8.3]] 

실행 3

[['Undefined', 7.5], ['Undefined', 7.5]] 
+0

'g'란 무엇입니까? 초기화 된 위치는 어디입니까? –

답변

1

당신이 pickle.dump에 잊지 않았다 일어나지 않는다 당신의 파일? 이것은 아마도 당신이 원하는 것일 것입니다 :

try: 
    openFile = open("times.txt", "rb") 
    runTimes = pickle.load(openFile) 
    openFile.close() 
except FileNotFoundError: 
    runTimes = [] 

runTimes.append([g.name, g.count]) 
openFile = open("times.txt", "wb") 
pickle.dump(runTimes, openFile) 
openFile.close() 

if len(runTimes) > 1: 
    print(runTimes)