[Tutor] saveing and loading text data
Jacob Mansfield
cyberjacob at googlemail.com
Wed May 13 15:08:13 CEST 2009
hi everyone, I'm a bit new here but i was wondering if someone could check
some of my code, it's not doing quite what it's meant to. the problem is
when you start the application again and enter the databox.txt to load from
thanks
Databox_2_0.py:
import sys, os
pygame.init()
def load(filename):
if filename != '':
e = 1
dec = "placeholder"
fic = open(filename, "r")
while dec != '':
num = str(e)
print "found " + num + " enteries"
dec = fic.readline(e)
databox[e] = dec
dec = fic.readline((e+1))
databox2[e] = dec
e = e+1
fic.close()
else:
return 0
def search():
print "\n"
x = 1
items = len(databox)
ins = items+1
while ins > x :
dac = databox[x]
dac2 = databox2[x]
x = x + 1
print dac + " " + dac2
print "\n\n"
def add():
dat = raw_input("First name.\n")
dat2 = raw_input("\nSecond name.\n")
items = len(databox)
ins = items+1
databox[ins] = dat
databox2[ins] = dat2
print "Done.\n\n"
def exitprog():
fic = open('databox.txt','w')
print "saveing\n"
x = 1
items = len(databox)
ins = items+1
while ins > x :
dac = databox[x]
dac2 = databox2[x]
x = x + 1
fic.write(dac)
fic.write(dac2)
fic.close()
print "goodbye"
pygame.time.delay(900)
exit()
databox = dict()
databox2 = dict()
go = raw_input("filename, blank for none.\n")
load(go)
while True:
print "Welcome to databox V2.0."
print " 1. Searth the database."
print " 2. Add a record."
print " 3. Exit."
inme = raw_input("Please make a selection.\n")
if inme == "1":
search()
elif inme == "2":
add()
elif inme == "3":
exitprog()
else:
print "input not recignised."
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20090513/eed2ae3e/attachment-0001.htm>
More information about the Tutor
mailing list