Member Avatar for leegeorg07

hi again i help with my carnival clubs fundraising and being lazy like i am, i wanted to make a program that would tell you how much to ask for after inputting information and would then save it into a text file.
using griboullis' help from my other thread about cPickle i have managed to complete that but firstly i can only use the data until i open the program again

i will add the code when i get home as i am in school at the moment :)

Member Avatar for leegeorg07

sorry i couldn't i post the code in the other post but here is the code

from pprint import pprint
TXTFILE = "takings.txt"

def dump_takings(takings):
  fout = open(TXTFILE, "rw")
  pprint(takings, fout)
  fout.close()

def load_takings():
  fin = open(TXTFILE, "rw")
  dic = eval(fin.read())
  fin.close()
  return dic

def user_interaction(takings):
  

  running = 1
  n = 1
  T = float(raw_input("what is the price of the item in pence?"))
  print
  while running == 1:
      
      A = float(raw_input("How may tickets does the customer want?"))
      print
      ta = T*A
      takings[n] = ta
      TC = T*A #Ticket price times ammt of tickets wanted
      TCG = TC/100 #Same as above but divided by 100
      if TC >= 100.0:
          print "Ask for ",TCG,"pounds"
      else:
          print "Ask for ",TC,"P"
          takings[n] = TC

      print
      c = float(raw_input("What did they pay with?(use 1.00, 2.00)"))
      print

      ch = c-TCG

      chp = c-TC/100
  
      if c < 1.00:
          print "Give ",chp,"P change"
      else:
          print "Give £",ch," change"
      takings[n] = TC
      n = n+1
      running = float(raw_input('press 1 to stay'))

  
def main():
  takings = load_takings()
  try:
    user_interaction(takings)
  finally:
    dump_takings(takings)
main()

it also has a txt file called takings.txt with the {} things in it

thanks in advance

Shoudn't you open the file with mode "r" in load_takings and with mode "w" in "dump_takings" ?

Using 'rw' as a file mode will give you an error!

Member Avatar for leegeorg07

yeah thanks that was when i was trying different things but ill try the r and w when i get home

A problem with your program is that each time you restart the program, it will overwrite takings[1], takings[2], etc . So you should consider replacing the line n=1 by n = 1 + len(takings) .

I also suggest that you modify load_takings so that you don't have to create the txt file manually the first time, like this:

import os.path

def load_takings():
    if not os.path.exists(TXTFILE):
        dump_takings({})
    fin = open(TXTFILE, "r")
    dic = eval(fin.read())
    fin.close()
    return dic
Member Avatar for leegeorg07

thanks it hasnt fixed my problem but youve helped me with transfering files

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.