users_db = [
{'id': 'ax123', 'money': 5},
{'id': 'z5541', 'money': 0}
]
def get_user_by_id(user_id):
for user in users_db:
if user['id'] == user_id:
return user
def send_money(from_id, to_id, amount):
from_user_a = get_user_by_id(from_id)
to_user_b = get_user_by_id(to_id)
if from_user_a and to_user_b:
if from_user_a['money'] >= amount:
from_user_a['money'] -= amount
to_user_b['money'] += amount
else:
print('the amount of money is large than your balance')
send_money('ax123', 'z5541', 2)
send_money('ax123', 'z5541', 2)
print(users_db)
Preview:
downloadDownload PNG
downloadDownload JPEG
downloadDownload SVG
Tip: You can change the style, width & colours of the snippet with the inspect tool before clicking Download!
Click to optimize width for Twitter