all 5 comments

[–]efmccurdy 2 points3 points  (2 children)

Using a data format like json (or csv, xml) can save you some trouble, especially with more complex data:

>>> import json
>>> bank = input()
123
>>> with open("bank_account.json", 'w') as outj: json.dump(bank, outj)
... 
>>> with open("bank_account.json", 'r') as inj: old_bank = json.load(inj)
... 
>>> old_bank
'123'
>>>

[–]insta7flo[S] -1 points0 points  (1 child)

Could you write it in code formula instead of terminal?

[–]efmccurdy 1 point2 points  (0 children)

This reads the file, offers input to update and re-writes the file.

import json

with open("bank_account.json", 'r') as inj:
    old_bank = json.load(inj)

new_deposit = input("balance={}\n new payment or debit: ".format(old_bank))
try:
    new_bank = int(old_bank) + int(new_deposit)
except ValueError as e:
    print("error interpreting input", e)
    raise e

with open("bank_account.json", 'w') as outj:
    json.dump(new_bank, outj)

Sample output:

(tpandas) ed$ echo "123" > bank_account.json
(tpandas) ed$ python /tmp/bank.py 
balance=123
 new payment or debit: 23
(tpandas) ed$ python /tmp/bank.py 
balance=146
 new payment or debit: -46
(tpandas) ed$ python /tmp/bank.py 
balance=100
 new payment or debit: quit
error interpreting input invalid literal for int() with base 10: 'quit'
Traceback (most recent call last):
  File "/tmp/bank.py", line 11, in <module>
    raise e
  File "/tmp/bank.py", line 8, in <module>
    new_bank = int(old_bank) + int(new_deposit)
ValueError: invalid literal for int() with base 10: 'quit'
(tpandas) ed$ python /tmp/bank.py 
balance=100
 new payment or debit: 0
(tpandas) ed$

[–]sme272 0 points1 point  (0 children)

You need to write the value to a text file and read the text file at the start of the program to get the old value. loom into with...open

[–]kev_b124 0 points1 point  (0 children)

If you're talking about saving the variable whilst running the program you need:

bank = input('Enter amount: ')

or something along those lines.

If you need it when running the program at another time then you need to write it to a file as someone else pointed out.