You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Python银行账户管理系统选创建账户时异常触发取款流程问题

银行账户管理系统菜单选择异常问题

问题现象

开发银行账户管理系统项目阶段1(单用户文件I/O实现)时出现流程异常:程序菜单提供3个操作选项,选择选项3「创建账户」时,本应提示输入用户名与PIN码完成账户创建,实际直接弹出取款金额输入提示,无法定位问题原因。

问题复现代码

# PHASE 1 (FILE I/O with One Customer)
# Initialization of Current Balance ** Current Balance is the same for all users for the sake of simplicity ** 
myMoney = open("current_balance.txt")
currentBalance = int(myMoney.readline())

# Creation of Bank Account and Notifying User(s) of Current Balance
class Bank_Account:
    def __init__(self):
        self.balance= currentBalance
        print("Welcome to Your Bank Account System!")
    # If statements for the different options 
    def options(self):
        ch = int(input("1. Withdraw Money From Your Account\n2. Deposit Money From Your Account\n3. Create an Account\nEnter a Choice: "))
        if ch == 1: 
            s.withdraw
        if ch == 2: 
            s.deposit
        if ch == 3: 
            s.create
    # Function to Deposit Money 
    def deposit(self):
        amount=float(input("Enter the amount you want to deposit: "))
        self.balance += amount
        print("Amount Deposited: ",amount)
    # Function to Withdraw Money
    def withdraw(self):
        amount = float(input("Enter the amount you want to withdraw: "))
        if self.balance>=amount:
            self.balance-=amount
            print("You withdrew: ",amount)
        else:
            print("Insufficient balance ")
        # Function to Create an Account 
    def create(self): 
        user = str(input("Enter your username:"))
        pin = int(input("Enter your pin number:" ))
        print("test")
    def display(self):
        print("Net Available Balance=",self.balance)

# Creating an object of class
s = Bank_Account()
# Calling functions with that class
s.options()
s.withdraw()
s.deposit()
s.create()
s.display()

# PHASE 2 (With Database)

问题根因

两处代码逻辑错误共同导致该异常:

  • 方法调用语法错误,菜单分支完全不生效:Python中调用实例方法必须在方法名后加()才会执行方法体。options()分支判断里写的s.withdraw、s.deposit、s.create只是获取了方法对象的引用,没有实际触发方法运行,不管选哪个菜单选项,分支内的逻辑都不会执行。
  • 菜单逻辑后硬编码了全流程强制调用:s.options()执行结束后,后续代码按顺序写死了s.withdraw()、s.deposit()、s.create()、s.display()的调用。不管菜单选什么,都会无条件先执行取款逻辑(这就是选创建账户却直接弹出取款金额输入的直接原因),再依次执行存款、创建账户、余额展示逻辑,菜单选择完全没有起到流程控制作用。

修复方案

  1. 修正options()方法内的分支调用逻辑:将全局对象s替换为类实例自身的self(避免全局变量耦合),给所有方法调用补上括号,操作完成后自动展示余额:
def options(self):
    ch = int(input("1. Withdraw Money From Your Account\n2. Deposit Money From Your Account\n3. Create an Account\nEnter a Choice: "))
    if ch == 1: 
        self.withdraw()
    if ch == 2: 
        self.deposit()
    if ch == 3: 
        self.create()
    self.display()
  1. 删除options()调用后硬编码的冗余方法调用,仅保留对象初始化和菜单触发逻辑:
# Creating an object of class
s = Bank_Account()
# Calling functions with that class
s.options()
  1. (可选优化)文件读取使用with上下文管理器,避免文件句柄泄漏:
with open("current_balance.txt") as myMoney:
    currentBalance = int(myMoney.readline())

内容的提问来源于stack exchange,提问作者kkurup

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.09.03 06:21:47