now add a seperate banking functionality to it, users can deposit, withdraw and check…
- User
now add a seperate banking functionality to it, users can deposit, withdraw and check their balance
Assistant
# Simple Calculator + Mini-Bank import os, json DATA_FILE = "bank.json" def load_balance(): return json.load(open(DATA_FILE)) if os.path.exists(DATA_FILE) else 0.0 def save_balance(b): json.dump(b, open(DATA_FILE, "w")) def calculator(): print("Simple Calculator\nOperations: + - * /\nType 'quit' to exit") while True: expr = input("calc> ").strip() if expr.lower() == "quit": break try: a, op, b = expr.split() a, b = float(a), float(b) res = {"+": a+b, "-": a-b, "*": a*b, "/": a/b if b else None}[op] print("Result:", res if res is not None else "Div by 0") except Exception: print("Invalid input (use: num op num)") def bank(): bal = load_balance() print("Mini-Bank\nCommands: deposit <amt>, withdraw <amt>, balance, back") while True: cmd = input("bank> ").strip().lower() if cmd == "back": break elif cmd == "balance": print(f"Balance: ${bal:.2f}") elif cmd.startswith("deposit "): try: amt = float(cmd.split()[1]) if amt <= 0: raise ValueError bal += amt save_balance(bal) print("Deposited.") except ValueError: print("Enter positive number.") elif cmd.startswith("withdraw "): try: amt = float(cmd.split()[1]) if amt <= 0 or amt > bal: raise ValueError bal -= amt save_balance(bal) print("Withdrawn.") except ValueError: print("Invalid amount.") else: print("Unknown command.") def main(): while True: mode = input("Mode (calc/bank/quit)> ").strip().lower() if mode == "calc": calculator() elif mode == "bank": bank() elif mode == "quit": break else: print("Choose calc, bank, or quit.") if __name__ == "__main__": main()Usage
- Run the script.
- Choose
calcfor the calculator orbankfor banking. - In bank mode:
•
deposit 100– adds 50
•balance– shows current balance
•back– return to mode selection - Type
quitat the mode prompt to exit.
Balance is persisted in
bank.jsonin the same directory.
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.