Python无类/模块实现银行系统:余额与现金无法更新问题
银行系统存款/取款后余额不更新的问题修复
问题概述
作为Python新手,尝试不使用类或特定模块实现银行系统,已完成登录、注册、查询余额/持有现金功能,但执行存款(deposit)或取款(withdraw)操作后,账户余额(balance)与持有现金(cash)无法更新到文件中。
账户数据格式(每行4个字段,空格分隔):
用户名 密码 余额 持有现金
示例:
aaa aaa 1 1 bkhg lkhg 1 1 terat rtrt 1 1 aa aa 1 1
核心问题分析
- 存款函数无文件更新逻辑:
deposit仅修改了全局变量,完全没有将新余额写入文件的代码。 - 取款函数文件更新逻辑混乱:计算完新的持有现金后,又将变量覆盖为文件旧值,且用追加模式写入,未替换原数据。
- 文件操作模式错误:更新账户数据需要读取全部内容,修改对应行后重新写入,而非追加。
- 注册字段缺失:注册时只写入3个字段,缺少持有现金字段,导致后续查询出错。
- 变量类型转换混乱:反复在字符串和整数间转换,易引发逻辑错误。
修复后的完整代码
# 全局变量,存储当前登录用户的余额和持有现金 current_balance = 0 current_cash = 0 USER_FILE = "accounts.txt" print("Welcome To the Bank") def withdraw(): global current_cash, current_balance while True: amounts_input = input("How much do you want to withdraw? $\n(exit) or (back) or (amount)\n") if amounts_input == "exit": quit() elif amounts_input == "back": break elif amounts_input.isdigit(): amounts = int(amounts_input) if 0 < amounts <= current_cash: # 更新内存数值:现金减少,余额增加 current_cash -= amounts current_balance += amounts # 更新文件数据 update_account_data() print(f"{amounts}$ Has been withdrawn into your account!") back_or_exit = input("If you want to go back (back) or If you want to exit (exit)\n") if back_or_exit == "back": break elif back_or_exit == "exit": quit() elif amounts <= 0: print("We cannot accept 0 or less than that!") else: print("You don't have enough cash with you!") else: print("Enter a valid number to withdraw!") def deposit(): global current_balance, current_cash while True: amount_input = input("How much amount you want to deposit? $\n(exit) or (back) or (amount)\n") if amount_input == "exit": quit() elif amount_input == "back": break elif amount_input.isdigit(): amount = int(amount_input) if 0 < amount <= current_balance: # 更新内存数值:余额减少,现金增加 current_balance -= amount current_cash += amount # 更新文件数据 update_account_data() print(f"{amount}$ Has been deposited from your account!") back_or_exit = input("If you want to go back (back) or If you want to exit (exit)\n") if back_or_exit == "back": break elif back_or_exit == "exit": quit() elif amount <= 0: print("We cannot accept 0 or less than that!") else: print("You don't have enough currency in your account!") else: print("Enter a valid number to deposit!") def update_account_data(): """将当前登录用户的最新余额和现金写入文件""" global username, password, current_balance, current_cash # 读取所有账户数据 with open(USER_FILE, "r") as f: lines = f.readlines() # 修改对应账户的行 updated_lines = [] for line in lines: line = line.strip() if not line: continue parts = line.split() if parts[0] == username and parts[1] == password: updated_line = f"{username} {password} {current_balance} {current_cash}\n" updated_lines.append(updated_line) else: updated_lines.append(line + "\n") # 重新写入文件 with open(USER_FILE, "w") as f: f.writelines(updated_lines) def find_balance_and_cash(): """查询当前登录用户的余额和现金,存入全局变量""" global username, password, current_balance, current_cash with open(USER_FILE, "r") as f: for line in f.readlines(): line = line.strip() if not line: continue parts = line.split() if parts[0] == username and parts[1] == password: current_balance = int(parts[2]) current_cash = int(parts[3]) return def login(): """验证登录信息,成功返回True,失败退出""" global username, password with open(USER_FILE, "r") as f: for line in f.readlines(): line = line.strip() if not line: continue parts = line.split() if parts[0] == username and parts[1] == password: print("Correct credentials!") return True print("Incorrect credentials.") quit() def register(): """注册新账户,默认余额和现金为1""" usernamee = input("Please input your desired name\n") passwordd = input("Please input your desired password\n") # 写入完整4个字段 with open(USER_FILE, "a") as f: f.write(f"{usernamee} {passwordd} 1 1\n") print("Registration successful!") # 登录/注册流程 while True: log_choice = input("(Login) or (Register) or (Exit)\n").lower() if log_choice == "login": username = input("Please enter your username\n") password = input("Please enter your password\n") login() find_balance_and_cash() break elif log_choice == "register": register() break elif log_choice == "exit": quit() else: print("Sorry It Seems Like You Have Typed The Wrong Answer :( Try Again!") print(f"You have {current_balance}$ in your account and {current_cash}$ cash with you!") # 主操作循环 while True: choice = input("Here is a choice for you\n(exit) (withdraw) (deposit)\n").lower() if choice == "deposit": deposit() print(f"Current status: {current_balance}$ in account, {current_cash}$ cash") elif choice == "withdraw": withdraw() print(f"Current status: {current_balance}$ in account, {current_cash}$ cash") elif choice == "exit": quit() else: print("Sorry it seems like you have typed wrong answer")
关键修改说明
- 新增
update_account_data函数:专门处理文件更新,通过读取所有行、修改目标行、重新写入的方式替换原数据,避免追加模式的问题。 - 修复存取款逻辑:明确存取款时的数值变化关系,操作后立即调用文件更新函数,移除混乱的变量覆盖逻辑,直接操作全局整数变量。
- 修复注册函数:写入完整4个字段,符合数据格式要求。
- 合并查询函数:将两个查询函数合并为一个,一次获取余额和现金,简化逻辑。
- 优化文件操作:使用
with语句自动管理文件生命周期,避免资源泄漏。
内容的提问来源于stack exchange,提问作者sainbileg
相关产品推荐
相关产品推荐

