Python ATM类实例变量self.pin无法匹配验证问题求助
Python ATM程序PIN验证失败问题解决
原程序代码
class Atm: def __init__(self): self.pin = "" self.balance = 0 def menu(self): ask = int(input(""" Select from the menu 1. Press 1 to set your pin 2. Press 2 to deposit 3. Press 3 to withdrawl 4. Press 4 to check balance 5. Press 5 to exit : """)) if ask == 1: self.create_pin() elif ask == 2: self.deposit() def create_pin(self): self.pin = input(" Set your pin : ") print(" Pin sucesfully set ") def deposit(self): print(self.pin) tmp = input(" Enter your pin : ") if tmp == self.pin: amount = float(input(" Enter your deposit amount : ")) self.balance = amount + self.balance print(" Amount Deposit sucessfully ") else: print(" Wrong Pin ") sbi = Atm() sbi.menu()
问题描述
编写上述ATM程序后,通过create_pin方法设置PIN后,在deposit方法验证时始终进入else分支,输出“Wrong Pin”。
复现步骤
- 运行程序
- 选择1设置PIN为1234
- 重新运行程序
- 选择2存款并输入PIN 1234,输出“Wrong Pin”
期望效果
输入的临时变量tmp与预设PIN匹配时,允许用户存款。
问题根源
不是deposit方法逻辑错误,而是程序内存生命周期问题:
每次运行程序都会重新创建Atm类实例sbi,上一次运行时设置的PIN仅存在于当时的内存中,程序结束后内存释放,数据丢失。第二次运行时self.pin回到初始空字符串"",输入的1234与空字符串不匹配,因此触发错误提示。
解决方案
方案1:单次运行内完成所有操作(无需持久化)
当前菜单仅处理一次用户选择就结束,修改为循环结构,让用户在一次运行中完成PIN设置、存款等操作,无需重启程序:
class Atm: def __init__(self): self.pin = "" self.balance = 0 def menu(self): while True: # 加入循环,持续显示菜单 ask = int(input(""" Select from the menu 1. Press 1 to set your pin 2. Press 2 to deposit 3. Press 3 to withdrawl 4. Press 4 to check balance 5. Press 5 to exit : """)) if ask == 1: self.create_pin() elif ask == 2: self.deposit() elif ask == 3: self.withdraw() elif ask == 4: self.check_balance() elif ask == 5: print("退出程序") break else: print("无效选项,请重新选择") def create_pin(self): self.pin = input("Set your pin: ").strip() # 去除输入前后空格 print("Pin successfully set") def deposit(self): if not self.pin: print("请先设置PIN") return tmp = input("Enter your pin: ").strip() if tmp == self.pin: amount = float(input("Enter your deposit amount: ")) self.balance += amount print("Amount deposited successfully") else: print("Wrong Pin") def withdraw(self): if not self.pin: print("请先设置PIN") return tmp = input("Enter your pin: ").strip() if tmp == self.pin: amount = float(input("Enter your withdrawal amount: ")) if amount <= self.balance: self.balance -= amount print("Amount withdrawn successfully") else: print("余额不足") else: print("Wrong Pin") def check_balance(self): if not self.pin: print("请先设置PIN") return tmp = input("Enter your pin: ").strip() if tmp == self.pin: print(f"当前余额: {self.balance:.2f}") else: print("Wrong Pin") sbi = Atm() sbi.menu()
修改说明:
- 菜单加入
while True循环,支持多次操作直到选择退出 - 输入时用
.strip()去除前后空格,避免误输入空格导致匹配失败 - 补充取款、余额查询功能,操作前先检查是否已设置PIN
方案2:持久化PIN和余额(重启程序后数据保留)
如果需要重启程序后仍保留数据,可通过JSON文件存储PIN和余额:
import json import os class Atm: def __init__(self): self.data_file = "atm_data.json" self.load_data() # 初始化时加载保存的数据 def load_data(self): if os.path.exists(self.data_file): with open(self.data_file, "r") as f: data = json.load(f) self.pin = data.get("pin", "") self.balance = data.get("balance", 0.0) else: self.pin = "" self.balance = 0.0 def save_data(self): data = {"pin": self.pin, "balance": self.balance} with open(self.data_file, "w") as f: json.dump(data, f) def menu(self): while True: ask = int(input(""" Select from the menu 1. Press 1 to set your pin 2. Press 2 to deposit 3. Press 3 to withdrawl 4. Press 4 to check balance 5. Press 5 to exit : """)) if ask == 1: self.create_pin() elif ask == 2: self.deposit() elif ask == 3: self.withdraw() elif ask == 4: self.check_balance() elif ask == 5: print("退出程序") break else: print("无效选项,请重新选择") def create_pin(self): self.pin = input("Set your pin: ").strip() self.save_data() # 设置PIN后保存数据 print("Pin successfully set") def deposit(self): if not self.pin: print("请先设置PIN") return tmp = input("Enter your pin: ").strip() if tmp == self.pin: amount = float(input("Enter your deposit amount: ")) self.balance += amount self.save_data() # 修改余额后保存数据 print("Amount deposited successfully") else: print("Wrong Pin") def withdraw(self): if not self.pin: print("请先设置PIN") return tmp = input("Enter your pin: ").strip() if tmp == self.pin: amount = float(input("Enter your withdrawal amount: ")) if amount <= self.balance: self.balance -= amount self.save_data() # 修改余额后保存数据 print("Amount withdrawn successfully") else: print("余额不足") else: print("Wrong Pin") def check_balance(self): if not self.pin: print("请先设置PIN") return tmp = input("Enter your pin: ").strip() if tmp == self.pin: print(f"当前余额: {self.balance:.2f}") else: print("Wrong Pin") sbi = Atm() sbi.menu()
修改说明:
- 加入JSON文件读写逻辑,实现数据持久化
- 每次修改PIN或余额后调用
save_data()保存到文件 - 程序启动时自动加载之前保存的数据,重启后仍可使用原有PIN和余额
内容的提问来源于stack exchange,提问作者Rishabh Shrivasatava
相关产品推荐
相关产品推荐

