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. · คลิก Run เพื่อดูผลลัพธ์ที่นี่