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

为什么Python银行管理系统代码执行完首个for循环后不执行第二个?

问题原因分析
  • 打开system.txt时使用了a+模式,该模式下文件指针默认定位在文件末尾,直接调用readlines()无法读取到任何内容,所以第二个for循环不会执行
  • 账号匹配逻辑错误:Account_NumberList是列表类型,和字符串类型的idc做相等判断永远返回False,就算循环能运行也匹配不到对应账号
  • 登录校验的else分支位置错误:当前else和if对齐,只要第一行账号密码不匹配就会直接调用menu()退出,根本不会遍历后续的账号,也走不到第二个for循环
  • 没有正确关闭system.txt文件,存在资源泄露风险
修正后代码
def LCustomerAccount():
    EnteredID = str(input("========== 请输入您的账户ID:"))
    EnteredPassword = str(input("========== 请输入您的密码:"))
    login_success = False
    
    # 校验账号密码
    with open("Customerlogin.txt", "r", encoding="utf-8") as B:
        for line in B.readlines():
            line = line.strip()
            if not line:
                continue
            id, pw = line.split("|", 1)
            if EnteredID == id and EnteredPassword == pw:
                print("========== 登录成功 ============")
                print("========== 欢迎用户 ============")
                login_success = True
                break
        # 所有账号遍历完都没匹配才报错
        else:
            print("账号或密码错误")
            menu()
            return
    
    # 登录成功后读取账户信息
    if login_success:
        with open("system.txt", "r", encoding="utf-8") as G:
            for line in G.readlines():
                line = line.strip()
                if not line:
                    continue
                idc, at, balance = line.split("|", 2)
                if idc == EnteredID:
                    print("您的账户类型:", at)
                    print("您的账户余额:", balance)
                    print("========== (选项1). 存款 ============")
                    print("========== (选项2). 取款 ============")
                    print("========== (选项3). 修改账户密码 ============")
                    print("========== (选项4). 退出登录 ============")
                    EnterOption = int(input("==========请输入您的选择"))
                    # 后续选项逻辑可在此补充

内容的提问来源于stack exchange,提问作者Lee Wen Dick

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.09.23 16:27:00