Making a save file?

Making a save file?

Postby spamynator_1 » Mon Sep 23, 2013 2:23 pm

Im trying to make a save file thing for my game, and im not entirely sure on writing to files. This is what i got soo far.
Code: Select all
print("WRITE TO FILE")
one = (0,0)
two = (1,1)
three = (2,2)
Savefile = open(kcufakcud.dik[w])

This returns. "kcufakcud" is not defined.
What am i missing?
spamynator_1
 
Posts: 63
Joined: Sun Mar 03, 2013 12:45 am

Re: Making a save file?

Postby metulburr » Mon Sep 23, 2013 2:26 pm

open() takes the filename argument as a string, the second argument defining write mode or read mode
New Users, Read This
version Python 3.3.2 and 2.7.5, tkinter 8.5, pyqt 4.8.4, pygame 1.9.2 pre
OS Ubuntu 14.04, Arch Linux, Gentoo, Windows 7/8
https://github.com/metulburr
User avatar
metulburr
 
Posts: 1097
Joined: Thu Feb 07, 2013 4:47 pm
Location: Elmira, NY

Re: Making a save file?

Postby spamynator_1 » Mon Sep 23, 2013 6:58 pm

Hate to break it to you but that does not help me at all.
spamynator_1
 
Posts: 63
Joined: Sun Mar 03, 2013 12:45 am

Re: Making a save file?

Postby micseydel » Mon Sep 23, 2013 7:08 pm

spamynator_1 wrote:Hate to break it to you but that does not help me at all.

You realize he's trying to help you, right? Try being nice.

Anyway... If the code you posted is all you have, then of course that's the error you'd get, since you didn't define that variable. That's a very, very, very basic Python problem and if you're still struggling with defining a variable or recognizing a NameError like that then you should revisit more basic tutorials before trying to handle files.

Also, always post your full traceback verbatim rather than a snippet or summary of the error message you get. This is what a traceback looks like
Code: Select all
Traceback (most recent call last):
  File "<pyshell#2>", line 1, in <module>
    kcufakcud
NameError: name 'kcufakcud' is not defined
Join the #python-forum IRC channel on irc.freenode.net!
User avatar
micseydel
 
Posts: 928
Joined: Tue Feb 12, 2013 2:18 am
Location: Mountain View, CA

Re: Making a save file?

Postby spamynator_1 » Mon Sep 23, 2013 7:14 pm

My apologize, i threw together some slightly better code maybe more usefull.
Code: Select all
x,y = (1,83)
class Save(object):
    def __init__(self):
        self.location = (x,y)
    def write(self):
        print("WRITE TO FILE")
        save = open('save.dik','w+')
        save.write(self.location)
variable = Save()
variable.write()


this returns
Code: Select all
WRITE TO FILE

Traceback (most recent call last):
  File "E:\Google Drive\Game\Notes\Writing to file.py", line 10, in <module>
    variable.write()
  File "E:\Google Drive\Game\Notes\Writing to file.py", line 8, in write
    save.write(self.location)
TypeError: expected a character buffer object
spamynator_1
 
Posts: 63
Joined: Sun Mar 03, 2013 12:45 am

Re: Making a save file?

Postby micseydel » Mon Sep 23, 2013 7:17 pm

The write() method expects a string, but you're passing it a tuple. You can do some string formatting using the tuple, of if you just want the string representation you can use
Code: Select all
save.write(str(self.location))

or in place of str() you can use repr(). They're equivalent in this case, and the difference is relatively subtle, I'd stick with str() unless you have good reason to use repr().
Join the #python-forum IRC channel on irc.freenode.net!
User avatar
micseydel
 
Posts: 928
Joined: Tue Feb 12, 2013 2:18 am
Location: Mountain View, CA

Re: Making a save file?

Postby metulburr » Mon Sep 23, 2013 7:17 pm

change to
Code: Select all
save.write(str(self.location))
New Users, Read This
version Python 3.3.2 and 2.7.5, tkinter 8.5, pyqt 4.8.4, pygame 1.9.2 pre
OS Ubuntu 14.04, Arch Linux, Gentoo, Windows 7/8
https://github.com/metulburr
User avatar
metulburr
 
Posts: 1097
Joined: Thu Feb 07, 2013 4:47 pm
Location: Elmira, NY

Re: Making a save file?

Postby spamynator_1 » Mon Sep 23, 2013 10:29 pm

Thank you everyone for you help. It now works!
spamynator_1
 
Posts: 63
Joined: Sun Mar 03, 2013 12:45 am


Return to Game Development

Who is online

Users browsing this forum: No registered users and 1 guest