-1

My assignment is to keep a record of the transactions of an ATM program in a text file. After every deposit or withdraw you must update the file with the transaction type, amount deposited/withdrawn, and updated balance. I am able to get the text file to print out the current balance but only for one deposit/withdraw. Additionally, when the program runs it is supposed to open the file with the transactions and find the current balance for the account, but each time I run the program the new text just replaces the old. Here is my code:

balancefile=open("balancefile.txt", "w")
balancefile.write("Starting balance is $10000 \n")
USER_BALANCE=10000
name=input("What is your name? ")
print(name +"," + " " + "Your current balance is: $" + str(USER_BALANCE))
while True:
    answer=input("Would you like to deposit, withdraw, check balance, or exit? ")
    if answer=="deposit" or answer== "Deposit":
        x= input("How much would you like to deposit? ")
        USER_BALANCE += float(x)
        print (name + "," + " " + "Your new balance is: $" + str(USER_BALANCE))
        balancefile.write("You have deposited an amount of $" + x + "." + " " + "Current balance is $" + str(USER_BALANCE) +"\n")
        continue
    elif answer== "withdraw" or answer== "Withdraw":
        y= input("How much would you like to withdraw? ")
        if float (y)<=USER_BALANCE:
            USER_BALANCE -= float(y)
            print (name + "," + " " + "Your new balance is: $" + str(USER_BALANCE))
            balancefile.write("You withdrew an amount of $" + y + "." + " " + "Current balance is $" + str(USER_BALANCE) + "\n")
            continue
        else:
            print ("Cannot be done. You have insufficient funds.")
    elif answer== "check balance" or answer== "Check Balance":
        print ("$" + str(USER_BALANCE))
    elif answer== "exit" or answer== "Exit":
        print ("Goodbye!")
        balancefile.close()
        break
    else:
        print ("I'm sorry, that is not an option")

Please help! To clarify, the original amount in the account is $10,000 but you are supposed to be able to re-enter the program after exiting using the last updated balance in the text file.

  • Try opening with the 'a' flag. – ergonaut Oct 25 '17 at 01:23
  • You need to open your file for appending with `balancefile=open("balancefile.txt", "a")` – 0TTT0 Oct 25 '17 at 01:23
  • You are also setting your balance to 10,000 at the start of the script, regardless of what balance was on file. You should do a check first - if there is no balancefile.txt, or if balancefile.txt doesn't have any balances recorded in it yet, then set your balance to 10,000. Otherwise, read the last recorded balance in the file and set the balance to that value. – Steven Walton Oct 25 '17 at 01:27

1 Answers1

0

As the commenters have noted, the file mode 'w' will truncate the file proir to write, and 'a' will open a file for appending. This is described in further detail in the Python documentation.

alex
  • 6,818
  • 9
  • 52
  • 103