How to keep and run the program always form the last value, so when I open again the program starts from the last amount

*Preformatted text
import random

amount = 100

loggedOn = True

while loggedOn:

selection = int(input("Select 1 for Deposit, 2 for Withdraw or 3 for Exit: "))

if not selection:

    break

if selection == 1:

    deposit = float(input("How much will you deposit? "))

    amount += deposit

    print(f"Deposit in the amount of ${format(deposit, '.2f')} ")

    print(f"Bank account balance ${format(amount, '.2f')} ")

elif selection == 2:

    withdraw = float(input("How much will you withdraw? "))

    amount -= withdraw

    print(f"Withdraw in the amount of ${format(withdraw, '.2f')} ")

    print(f"Bank account balance ${format(amount, '.2f')} ")

else:

    loggedOn = False

print("Transaction number: ", random.randint(10000, 1000000))

Programs that run use RAM (random access memory), this data is released back to the OS (operating system) after the program is finished executing.

in order to persist data, we would need to write data to the hard drive. Which can be done using a simple file or a database.

3 Likes

Hi stetim94, thanks for your answer, I´m trying to use a json or pickle import to do it but it seems a bit confuse to me, so I was wondering an easier way to do this. thanks a lot!

if you are purely interested in amount, you could simple write the value to a file directly.

if you need some kind of structure to your file, json seems like the better choice. with pickle I get a lot of warnings about security issues

doesn’t seem to difficult:

https://wiki.python.org/moin/UsingPickle

has working with files been covered? If it hasn’t, maybe you should find a tutorial on working with files in python.

Hi Stetim94, thanks for your support again, I think I´m doing a wrong approach with the code because all I need is to Update or override amount = 100 every time I run the code and keep that updated value, if I save it via jason or in a file it will return me the last amount saved but start again from amount = 100, so this code doesn´t work as a banking account. thanks anyways. :love_you_gesture:

I don’t see the problem? We could simple overwrite/update the value stored in the file. Doesn’t matter whether you use json or not.

Could this be because you’ve kept the line amount = 100 in your code? Make sure you set amount to the amount in whichever file you’ve saved the value to.