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. · לחץ על הרץ כדי לראות את התוצא כאן.