Today's Posts Follow Us On Twitter! TFL Members on Twitter  
Forum search: Advanced Search  
Navigation
Marketplace
  Members Login:
Lost password?
  Forum Statistics:
Forum Members: 24,254
Total Threads: 80,792
Total Posts: 566,471
There are 1154 users currently browsing (tf).
 
  Our Partners:
 
  TalkFreelance     Design and Development     Programming     Other Programming Languages :

[Python] Working with a save function.

Thread title: [Python] Working with a save function.
Closed Thread    
    Thread tools Search this thread Display Modes  
07-12-2007, 04:21 PM
#1
xZaft is offline xZaft
Status: Member
Join date: Jul 2005
Location: Massachusetts, US
Expertise:
Software:
 
Posts: 428
iTrader: 0 / 0%
 

xZaft is on a distinguished road

  Old  [Python] Working with a save function.

Code:
def save():
    try:
        saves.append(str(level))
        saves.append(str(stre))
        saves.append(str(spd))
        saves.append(str(hp))
        saves.append(str(maxhp))
        saves.append(str(exp))
        saves.append(str(maxexp))
        saves.append(str(gp))
        saves.append(str(sword))
        save = open('gamesave.txt', 'r+')
        for s in saves:
            save.write(s)
            save.write('\n')
            saves.remove(s)
            print s
        print "Save successful."
        save.close()
    except ValueError: print "Save unsuccessful."
This should append all of those values, all of which are numbers, into the saves list. Then it should open gamesave.txt, and write them into it, and remove them from the list one by one. As it goes through, only a few are actually sent through, and the last 4 are left.

The attachment shows this. The top line is the saves list. The numbers below it are the ones that are written to the text file.

Anyone know why it won't get all the way through the list?

Attached Images
File Type: jpg gamesave.jpg (18.2 KB, 9 views)

07-13-2007, 04:08 AM
#2
eldafar is offline eldafar
Status: I'm new around here
Join date: Jun 2007
Location:
Expertise:
Software:
 
Posts: 13
iTrader: 0 / 0%
 

eldafar is on a distinguished road

  Old

Originally Posted by xZaft View Post
Code:
def save():
    try:
        saves.append(str(level))
        saves.append(str(stre))
        saves.append(str(spd))
        saves.append(str(hp))
        saves.append(str(maxhp))
        saves.append(str(exp))
        saves.append(str(maxexp))
        saves.append(str(gp))
        saves.append(str(sword))
        save = open('gamesave.txt', 'r+')
        for s in saves:
            save.write(s)
            save.write('\n')
            saves.remove(s)
            print s
        print "Save successful."
        save.close()
    except ValueError: print "Save unsuccessful."
This should append all of those values, all of which are numbers, into the saves list. Then it should open gamesave.txt, and write them into it, and remove them from the list one by one. As it goes through, only a few are actually sent through, and the last 4 are left.

The attachment shows this. The top line is the saves list. The numbers below it are the ones that are written to the text file.

Anyone know why it won't get all the way through the list?
Why does it fail? Because it's not a good idea to modify the list you are looping through with for. Why do you need to run remove on every item in the list if you are going to append every string in it into the file, since saves is supposed to be empty at the end anyways, why touch it? Just create a new saves. That is of course if you really want to empty the saves list. But you shouldn't have any reason to do so, because it's in a method, it's a local variable, it's not going to be used again after the call on method and will be picked up by the garbage collector. So don't worry about it.

Solution:
Code:
def save():
    try:
       #btw: the way you put stuff into your list isn't exactly effecient
        saves.append(str(level))
        saves.append(str(stre))
        saves.append(str(spd))
        saves.append(str(hp))
        saves.append(str(maxhp))
        saves.append(str(exp))
        saves.append(str(maxexp))
        saves.append(str(gp))
        saves.append(str(sword))
        save = open('gamesave.txt', 'r+')
        for s in saves:
            save.write(s+'\n')
            print s
        print "Save successful."
        save.close()
    except ValueError: print "Save unsuccessful."
Cheers!

Closed Thread    


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 

  Posting Rules  
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump:
 
  Contains New Posts Forum Contains New Posts   Contains No New Posts Forum Contains No New Posts   A Closed Forum Forum is Closed