I am making a game as a programmer that has only worked with python for about 2 months, and I need a way to save data. I know about pickle, but the Documentation does not help at all. Please note that I am using 3.1.1, so don't give me answers for python 2.x. Here is some of the code (Also note that I have used underscores were I need help):

import time
import pickle
import random
#town 1 is Marata, town 2 Khmerville, 3 rome, 4 babel, and 5 is Startville
def saveConduct():
    _______________________
____________________________

def townselect():
    print('Which town would you like to go to?')
    print('Marata (1), khmerville (2), Rome (3), or Babel (4)?')
    if input() == '1':
        print('See Marata, a prosperous City. Shop here?')
        if input() == 'yes' or 'Yes' or 'y' or 'Y':
            print('Your options are:')
    print('Pork(p), $15 a pound')
    print('Fertilizer(f), $12 a pound')
    print('Tulip Bulbs(t), 6000 florins a pound')#A Little Joke
    if input() == 'p':
        print('What quantity? 1, 10, or 50?')
        if input() == '1':
            money = money - 15
            goodspace = goodspace - 1 
            print('your balance is ' , money)
            Pork = 1
        elif input() == '10':
            money == money - 150
            goodspace = goodspace - 10
            print('Your balance is' , money)
            Pork = 10
        else:
            money = money - 750
            goodspace = goodspace - 50
            print ('Your balance is ' , money)
            Pork = 50
    elif input() == 's':
        print('What Quantity? 1, 10, or 50?')
        if input() == '1':
            money = money - 100
            print (money)
        elif input() == '10':
            money = money - 1000
            goodspace = goodspace - 10
            print('your balance is ' , money)
        elif input() == '50' and money >= 5000:
            money = money - 5000
            goodspace = goodspace - 50
            print ('Your balance' , money)
        elif input is '50' and money < 5000:
            print("You don't have enough money for that!")
            playing = False
    elif input() == 'S':
        print('What Quantity? 1, 10, or 50?')
        if input() == '1':
            money = money - 1500
            goodspace = goodspace - 1
            print ('Your balance' , money)
            Spice = 1
        elif input == '10' and money >= 15000:
            money = money - 15000
            goodspace = goodspace - 10
            Spice = 10
            print ('Your balance' , money)
        elif input() == 50 and money >= 75000:
            money = money - 75000
            goodspace = goodspace - 50
            print ('Your balance' , money)
    else:
        print("Dude, Work on yur speleing.")
        print('I CAN spele, jest so u no')
        print("NOW I'm spelling stuff correctly.")
        time.sleep(1)
        quit
def game():    
    money = 5000
    goodspace = 50
    playing = True
    print('Good. lets start the game\n\
But First, a Note: 1$ = 60 Florins in game.')#---IMPORTANT---#
    time.sleep(3)
    print('You are a poor man,')
    print('Trying to make a fortune.')
    time.sleep(1)
    print('You start with $5000,')
    print('And your goal is $1,000,000.')
    time.sleep(1)
    print('You can choose between 3 difficultys.')
    time.sleep(1)
    print('These difficultys are:')
    print('Easy (e), medium (Unavailable), and hard(Unavailable). choose one.')
    if input() == 'e':
        print('You have selected easy mode. In this mode, there are less disasters')
        time.sleep(1)
        print('Ready...')
        time.sleep(1)
        print('Set...')
        time.sleep(1)
        print('GO!')
        print("You're in a shop. Would you like to buy something?")
        if input() == 'yes' or 'Yes' or 'y' or 'Y':
            town = '1'
            print('your options are:')
            print('Wool(w), $10 a pound')
            print('Sugar(s), $100 a pound')
            print('Spices(S), $1500 a pound')
            if input() == 'w':
                print('What quantity? 1, 10, or 50?')
                if input() == '1':
                    money = money - 10
                    goodspace = goodspace - 1 
                    print('your balance is ' , money)
                elif input() == '10':
                    money == money - 100
                    goodspace = goodspace - 10
                    print('Your balance is' , money)
                else:
                    money = money - 500
                    goodspace = goodspace - 50
                    print ('Your balance is ' , money)
            elif input() == 's':
                print('What Quantity? 1, 10, or 50?')
                if input() == '1':
                    money = money - 100
                    print (money)
                elif input() == '10':
                    money = money - 1000
                    goodspace = goodspace - 10
                    print('your balance is ' , money)
                elif input() == '50' and money >= 5000:
                    money = money - 5000
                    goodspace = goodspace - 50
                    print ('Your balance' , money)
                elif input is '50' and money < 5000:
                    print("You don't have enough money for that!")
                    playing = False
            elif input() == 'S':
                print('What Quantity? 1, 10, or 50?')
                if input() == '1':
                    money = money - 1500
                    goodspace = goodspace - 1
                    print ('Your balance' , money)
                elif input == '10' and money >= 15000:
                    money = money - 15000
                    goodspace = goodspace - 10
                    print ('Your balance' , money)
                elif input() == 50 and money >= 75000:
                    money = money - 75000
                    goodspace = goodspace - 50
                    print ('Your balance' , money)
            else:
                print("Dude, Work on yur speleing.")
                print('I CAN spele, jest so u no')
                print("NOW I'm spelling stuff correctly.")
                time.sleep(1)
                quit
        elif input() == 'no':
            townselect
    print("Save Game?")
    if input() == 'yes':

This is the point were I want to use a variable.

Recommended Answers

All 5 Replies

Update: I was fooling around in a program i use as a sort of testing ground for new ideas (Or better variations of old Ideas) in my game, and I got an error using the following code:

import pickle
import time
filler = 123
Num1 = 19
Num2 = 92
Num3 = 47
Num4 = 13
Num5 = 1
saveDict = {Num1 : Num1, Num2 : Num2, Num3 : Num3}
pickle.dump(saveDict, filler, open('save.p', 'wb'))
time.sleep(1)
print('Open Saved Game?')
if input() == 'yes':
    pickle.load(saveDict, open('save.p', 'rb'))
    print (saveDict)
else:
    quit

Here is the error I got:

Traceback (most recent call last):
  File "G:\saveTest.py", line 10, in <module>
    pickle.dump(saveDict, filler, open('save.p', 'wb'))
  File "C:\Python31\lib\pickle.py", line 1354, in dump
    Pickler(file, protocol, fix_imports=fix_imports).dump(obj)
TypeError: an integer is required

You got zillion of input functions without prompts in your first code, it couldn't possibly be debugged enough to add more features. You must allways be sure that the old code is as bug free as possibly can, before adding things or you end up in mess (and looks like your code is a mess, and maybe better go back to last surely correct version, and start over).

Function input can never be '50' for example at line 133, and is identity check function is not used in this context anyway. And condition on line 100 is allways True as 'Yes' is a True value (not empty or zero).

In second code you are giving a file handle as a protocol for pickle, what you try to do?
Also you would be calling pickle.load with two parameters when it takes only one, also you do not save the value in any variable so it would have no effect anyway.

Here is an example how to use the module pickle:

# use module pickle to save/dump and load a dictionary object
# or just about any other intact object
# use binary file modes "wb" and "rb" to make it work with
# Python2 and Python3

import pickle

# create the test dictionary
before_d = {}
before_d[1]="Name 1"
before_d[2]="Name 2"
before_d[3]="Name 3"

# pickle dump the dictionary
fout = open("dict1.dat", "wb")
# default protocol is zero
# -1 gives highest prototcol and smallest data file size
pickle.dump(before_d, fout, protocol=0)
fout.close()

# pickle load the dictionary
fin = open("dict1.dat", "rb")
after_d = pickle.load(fin)
fin.close()

print( before_d )  # {1: 'Name 1', 2: 'Name 2', 3: 'Name 3'}
print( after_d )   # {1: 'Name 1', 2: 'Name 2', 3: 'Name 3'}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.