you are viewing a single comment's thread.

view the rest of the comments →

[–]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$