I'm using Python 2.7.8 and I'm trying to code a simple text-based game. In order for it to work well, I'd like to be able to save certain variables into a text file and then load them back in as variables the next time I run the program. Ideally, I'd like to store each variable on its own line but I'm not quite sure how to make that happen. EDIT: Here's my relevant code:
https://gist.github.com/anonymous/3a14fcd68b0045b2c0c0
For some reason, the code crashes at launch. Apologies because I'm a little new to Python. Anyway, is there a better way to write this information to a file? In addition, what could I use to read a variable from that file and store it as a variable in the game?
EDIT: I have now learned something incredibly useful which allows me to debug. Here is what the shell says when the code is run:
https://gist.github.com/anonymous/bfc085d8d1d0b7969574
Thanks so much.
ConfigParserexamples entry in the Python wiki - should be exactly what you need.#setting up save file savefile = "C:\Users\Slick\Documents\python\save.ini" FILE = open(savefile,"w") Config.add_section('info') countryname = raw_input("What will your country be called? ") print "Welcome to " + countryname + "!" time.sleep(0.5) Config.set('info','name',countryname) Config.write(savefile)At the end, I also have:FILE.close()The file crashes at launch. What am I doing wrong?UnboundLocalError: local variable 'countryname' referenced before assignment, if I say "y" to the question on line 29, it's going to execute lines 31-35, which don't definecountryname, then it's going to skip down to line 54, where it tries to usecountryname. You need to add code to the block on line 31-35 to setcountryname. Also, the code is still a bit off in the pastebin (see line 39, I don't think you want that line there).