如何为Python简易ATM程序添加5000元最低账户余额限制?
简易ATM程序最低余额管控及账户限制实现方案
一、新增取款后最低余额5000限制
方式1:直接用if-else语句修改取款逻辑
在原代码的取款分支(opt == 2)中,增加对取款后余额的判断,确保余额不低于5000,同时优化输入校验避免程序报错:
import time print("Welcome to OtnisBank") print("Please insert your card...") time.sleep(5) pwd = 1234 pin = int(input("Please enter your security pin: ")) bal = 737000 transaction_Num = 20221000114 while True: if pin == pwd: print(""" ******************************************** 1) Balance 2) Withdraw 3) Deposit 4) Cancel Transaction ******************************************** """) try: opt = int(input("Please choose your transaction ")) except ValueError: print("Invalid input, input must be an integer.") continue if opt == 1: print(f"Your current balance is ₱{bal}.00") if opt == 2: print("Maximum of 100k per transaction | Minimum balance after withdrawal: ₱5000.00") try: withdraw = int(input("Please enter amount: ")) except ValueError: print("Invalid input, amount must be an integer.") continue # 检查单笔取款上限 if withdraw > 100000: print("Invalid amount, maximum of 100k per transaction.") # 检查取款后余额是否达标 elif bal - withdraw < 5000: max_withdraw = bal - 5000 print(f"Invalid amount, balance must remain at least ₱5000.00. You can withdraw up to ₱{max_withdraw}.00") else: bal = bal - withdraw print(f"₱{withdraw}.00 is withdrawn from your account...") print(f"Your updated balance is ₱{bal}.00") if opt == 3: try: deposit = int(input("How much money would you like to deposit?: ")) except ValueError: print("Invalid input, amount must be an integer.") continue bal = bal + deposit print(f"₱{deposit}.00 is deposited unto your account...") print(f"Your updated balance is ₱{bal}.00") if opt == 4: print("Transaction success...") print(f"Transaction number: {transaction_Num + 1}") print( "Thank you for choosing our bank. Invest in the red, it's in your best interest.") break else: print("Please enter a valid pin and try again.") break
方式2:封装为可复用函数
把取款校验逻辑封装成独立函数,便于后续维护和规则修改:
import time def can_withdraw(balance, amount, max_single=100000, min_balance=5000): """校验取款是否合法,返回(是否允许, 提示信息)""" if amount <= 0: return False, "Amount must be a positive integer." if amount > max_single: return False, "Exceeds maximum single withdrawal limit (₱100000.00)." if balance - amount < min_balance: return False, f"Insufficient remaining balance, must keep at least ₱{min_balance}.00." return True, "Withdrawal allowed." print("Welcome to OtnisBank") print("Please insert your card...") time.sleep(5) pwd = 1234 pin = int(input("Please enter your security pin: ")) bal = 737000 transaction_Num = 20221000114 while True: if pin == pwd: print(""" ******************************************** 1) Balance 2) Withdraw 3) Deposit 4) Cancel Transaction ******************************************** """) try: opt = int(input("Please choose your transaction ")) except ValueError: print("Invalid input, input must be an integer.") continue if opt == 1: print(f"Your current balance is ₱{bal}.00") if opt == 2: print("Maximum of 100k per transaction | Minimum balance after withdrawal: ₱5000.00") try: withdraw = int(input("Please enter amount: ")) except ValueError: print("Invalid input, amount must be an integer.") continue allowed, msg = can_withdraw(bal, withdraw) if not allowed: print(msg) else: bal -= withdraw print(f"₱{withdraw}.00 is withdrawn from your account...") print(f"Your updated balance is ₱{bal}.00") if opt == 3: try: deposit = int(input("How much money would you like to deposit?: ")) except ValueError: print("Invalid input, amount must be an integer.") continue bal += deposit print(f"₱{deposit}.00 is deposited unto your account...") print(f"Your updated balance is ₱{bal}.00") if opt == 4: print("Transaction success...") print(f"Transaction number: {transaction_Num + 1}") print( "Thank you for choosing our bank. Invest in the red, it's in your best interest.") break else: print("Please enter a valid pin and try again.") break
二、余额低于5000的账户限制/注销逻辑
1. 账户交易限制
当余额低于5000时,限制取款操作,仅允许查询余额和存款:
# 在pin验证通过后增加余额检查 if pin == pwd: # 新增账户限制提示 if bal < 5000: print("⚠️ NOTICE: Your account balance is below the minimum required (₱5000.00). Only balance inquiry and deposit are allowed.") # 原有菜单打印逻辑... # 在取款分支增加限制判断 if opt == 2: if bal < 5000: print("Account restricted: Withdrawal is not allowed due to low balance.") continue # 原有取款校验逻辑...
2. 账户注销实现
模拟账户注销逻辑,当余额低于5000时提示用户,支持立即注销或触发自动注销规则:
import time def can_withdraw(balance, amount, max_single=100000, min_balance=5000): if amount <= 0: return False, "Amount must be a positive integer." if amount > max_single: return False, "Exceeds maximum single withdrawal limit (₱100000.00)." if balance - amount < min_balance: return False, f"Insufficient remaining balance, must keep at least ₱{min_balance}.00." return True, "Withdrawal allowed." print("Welcome to OtnisBank") print("Please insert your card...") time.sleep(5) pwd = 1234 pin = int(input("Please enter your security pin: ")) bal = 737000 transaction_Num = 20221000114 account_active = True # 新增账户状态标记 while True: if not account_active: print("Account has been closed. No transactions are allowed.") break if pin == pwd: # 余额低于5000时触发注销提示 if bal < 5000: print("⚠️ WARNING: Your account balance is below ₱5000.00. Account will be automatically closed in 30 days if not replenished.") close_choice = input("Would you like to close your account immediately? (Y/N): ").strip().upper() if close_choice == 'Y': print(f"Account closed successfully. Your remaining balance ₱{bal}.00 will be issued as a paper check within 7 business days.") account_active = False bal = 0 break print(""" ******************************************** 1) Balance 2) Withdraw 3) Deposit 4) Cancel Transaction ******************************************** """) try: opt = int(input("Please choose your transaction ")) except ValueError: print("Invalid input, input must be an integer.") continue if opt == 1: print(f"Your current balance is ₱{bal}.00") if opt == 2: if bal < 5000: print("Account restricted: Withdrawal is not allowed due to low balance.") continue print("Maximum of 100k per transaction | Minimum balance after withdrawal: ₱5000.00") try: withdraw = int(input("Please enter amount: ")) except ValueError: print("Invalid input, amount must be an integer.") continue allowed, msg = can_withdraw(bal, withdraw) if not allowed: print(msg) else: bal -= withdraw print(f"₱{withdraw}.00 is withdrawn from your account...") print(f"Your updated balance is ₱{bal}.00") if opt == 3: try: deposit = int(input("How much money would you like to deposit?: ")) except ValueError: print("Invalid input, amount must be an integer.") continue bal += deposit print(f"₱{deposit}.00 is deposited unto your account...") print(f"Your updated balance is ₱{bal}.00") if opt == 4: print("Transaction success...") print(f"Transaction number: {transaction_Num + 1}") print( "Thank you for choosing our bank. Invest in the red, it's in your best interest.") break else: print("Please enter a valid pin and try again.") break
内容的提问来源于stack exchange,提问作者レッド・ スワン
相关产品推荐
相关产品推荐

