Bank account · 银行账户
Bank account
A tiny bank that follows a list of instructions and never lets the balance go below zero.
"deposit 50".split() breaks the string at its spaces into ["deposit", "50"]. The amount is still text, so convert it with int(). Check that there is enough money before a withdrawal changes the balance; a refused withdrawal simply does nothing.
银行账户
一个很小的银行:按照一串指令操作,并且绝不让余额低于零。
"deposit 50".split() 按空格把字符串拆成 ["deposit", "50"]。金额仍然是文字,所以要用 int() 转换。在取款改变余额之前,先检查钱够不够;被拒绝的取款什么也不做。
Write final_balance(actions). Each action is a string such as "deposit 50" or · 或 "withdraw 20". Start at 0, apply the actions in order, and refuse any withdrawal that would take the balance below 0. Return the final balance. · 编写 final_balance(actions)。每个操作是一个字符串,例如 "deposit 50" 或 "withdraw 20"。余额从 0 开始,按顺序执行这些操作,拒绝任何会让余额低于 0 的取款。返回最终余额。
Click Run to see the output here. · 点击“运行”查看此处输出。