import random import threading accounts = {c:0 for c in 'abcdefghijklmnopqrstuvwxyz'} holders = tuple(accounts.keys()) def deposit(account, amount): if accounts[account] + amount < 0: return False accounts[account] += amount def banker(): for k in range(10000000): a = random.choice(holders) deposit(a, random.randrange(-100, 50)) if any(v < 0 for v in accounts.values()): raise Exception("Negative balance!") t1 = threading.Thread(target=banker) t2 = threading.Thread(target=banker) t1.start() t2.start() t1.join() t2.join() print(accounts)