I am currently creating a text-based game with ingame money, and I want a way for users to store their information so that they can log on later. Is this possible without connecting to the Internet?
I was stuck on this problem for a week before I found out about pickle.
To load the information
import pickle
from getpass import getpass
username = raw_input('What would you like your username to be? ')
username2 = raw_input('Please enter the same username again: ')
while username != username2:
print 'The usernames do not match.'
username = raw_input('What would you like your username to be? ')
username2 = raw_input('Please enter the same username again: ')
pword = getpass("What would you like your password to be? ")
pword2 = getpass("Please reenter the password: ")
while pword != pword2:
print 'The passwords do not match'
pword = getpass('What would you like your password to be? ')
pword2 = getpass('Please reenter the password: ')
money_left = 0
logininfo = [username, pword, money_left]
pickle.dump(logininfo, open( "%s.p" %username, "wb"))
print 'Your username is %s, and your password is %s. You have $%d ingame money.' % (username, pword, money_left)
To retrieve the information
import pickle
from getpass import getpass
login = raw_input('Enter your username')
password = getpass("Enter your password: ")
if pickle.load(open("%s.p"%login, "rb"))[1] == password:
username = pickle.load(open("%s.p"%login, "rb"))[0]
pword = pickle.load(open("%s.p"%login, "rb"))[1]
money_left = pickle.load(open("%s.p"%login, "rb"))[2]
print 'You have successfully logged in as %s and you have $%d ingame money.' %(login, money_left)
This should work, it worked for me. The getpass is optional, but it prevents character echo when you're entering your password. You can also change the amount of 'money_left'. This basically creates a file named "%s.p" %username. So if your username is pickler, it will create a file called pickler.p.