# See extensive tutorial here https://docs.python.org/3/library/sqlite3.html import time import sqlite3 import sys def get_balance(cur: sqlite3.Cursor, usr: str) -> float: res = cur.execute("SELECT balance FROM acc WHERE usr=?", [usr]) row = res.fetchone() b = row[0] return b def set_balance(cur: sqlite3.Cursor, usr: str, b1: float) -> None: cur.execute(""" UPDATE acc SET balance = ? WHERE usr=? """, [b1, usr]) def main() -> None: con = sqlite3.connect("bank.db", autocommit=True) cur = con.cursor() res = cur.execute("SELECT * FROM acc") answ = res.fetchall() print(answ) usr = input("Enter the user name: ") res = cur.execute("SELECT * FROM acc WHERE usr=?", [usr]) if res.fetchone() is None: print("Wrong user. Exit") sys.exit() while True: try: print() print() cmd = input(usr + ": (b)alance/(d)eposit/(w)ithdraw/" "(t)ransfer/(q)uit: ") if cmd == "b": b = get_balance(cur, usr) print("Your balance is ", b) elif cmd == "d": amount = input(usr + ": insert cash or check into the machine." " Amount: ") a = float(amount) b = get_balance(cur, usr) b1 = b+a set_balance(cur, usr, b1) print(usr + ": your new balance is ", b1) elif cmd == "w": amount = input(usr + ": how much? ") a = float(amount) b = get_balance(cur, usr) if a > b: print("ERROR: INSUFFICIENT FUNDS!!") else: for x in range(4): print("***DISPENSING MONEY TO "+usr+" NOW***") time.sleep(1) b1 = b-a set_balance(cur, usr, b1) print(usr+": your new balance is ", b1) elif cmd == "t": amount = input(usr + ": how much do you transfer? ") a = int(amount) b = get_balance(cur, usr) if a > b: print("ERROR: INSUFFICIENT FUNDS!!") else: usrt = input(usr+": to whom do you transfer? ") bt = get_balance(cur, usrt) b1 = b-a bt1 = bt+a set_balance(cur, usr, b1) set_balance(cur, usrt, bt1) print(usr + ": your new balance is ", b1) print(usrt + ": your new balance is ", bt1) elif cmd == "q": sys.exit() except EOFError: sys.exit() if __name__ == '__main__': main()