Jacob Mansfield wrote:
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."

------------------------------------------------------------------------

_______________________________________________
Tutor maillist  -  [email protected]
http://mail.python.org/mailman/listinfo/tutor
Hi there,

I would personally change your load method to something like this:

def load(filename):
   try:
       fIn = open(filename, 'rb')
   except OSError:
       return 0

   for idx, line in enumerate(fIn):
       rec_cnt = (idx / 2) + 1
       if not idx % 2:
           databox[rec_cnt] = line.strip()
       else:
           databox2[rec_cnt] = line.strip()
   fIn.close()


The problem with your original load was the fact that using file.readline( a_number ) does a readline of a specific size. You could of done it with just a .readline() which would return the next line for you.

I changed the stucture of it to look cleaner, you now enumerate over the file object which returns you a counter as well as yielding the next line. The record counter will track the correct record (from your original post you had Name on one line and Surname on the following line so with that in mind it will now split it between the two dictionaries based on that.

Hope that helps get you in the right direction.

--
Kind Regards,
Christian Witts


_______________________________________________
Tutor maillist  -  [email protected]
http://mail.python.org/mailman/listinfo/tutor

Reply via email to