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

Python3 ATM程序PIN校验逻辑异常问题求助

ATM PIN校验失败问题排查与修复

问题根源

不是全局列表的问题,核心是类型不匹配:

  • pin_no列表里存储的是整数(如1234)
  • input()获取的用户输入是字符串(如"1234")
    Python中不同类型的值无法直接匹配,所以无论输入正确PIN,inp_pin in pin_no都会返回False。

另外代码还有两个小问题:

  1. 重试次数计算错误:初始设c=3,第一次错误后应该提示剩余2次,当前逻辑会错误显示剩余1次
  2. 重试输入正确PIN后,没有终止循环,程序会继续执行后续代码

修复后的代码

pin_no = [1234, 4321, 9876]     # Neil, John, Sue

class Account:
    def __init__(self, balance, name, cash_in_hand):
        self.name = name
        self.balance = balance
        self.cash_in_hand = cash_in_hand

def checker():
    proceed_1 = False
    proceed_2 = False
    end = False

    while proceed_1 == False and proceed_2 == False and end == False:
        print('Card Inserted?')
        inp_card = input('Y / N > ')

        if inp_card.lower() == 'y':
            proceed_1 = True
            print('\nPlease input your PIN')
            # 处理输入转换,同时捕获非数字输入的异常
            try:
                inp_pin = int(input('PIN > '))
                if inp_pin in pin_no:
                    proceed_2 = True
                    welcome()
                    end = True  # 登录成功后终止循环
                else:
                    remaining_attempts = 2
                    while remaining_attempts > 0:
                        print(f'Incorrect Pin. You have {remaining_attempts} attempt/s left.')
                        try:
                            retry = int(input('PIN > '))
                            if retry in pin_no:
                                welcome()
                                end = True
                                break
                            else:
                                remaining_attempts -= 1
                        except ValueError:
                            print('请输入数字PIN')
                            remaining_attempts -= 1
                    if remaining_attempts == 0:
                        print('尝试次数耗尽,卡已锁定')
                        end = True
            except ValueError:
                print('请输入数字PIN')

        elif inp_card.lower() == 'n':
            print('Please input your card')
        else:
            print('Invalid Option!')

def welcome():
    print('Welcome')
    print('---------------')
    print('1. Check Balance')
    print('2. Withdraw funds')
    print('3. Deposit funds')
    print('4. Exit')
    response = input('Choice> ')
    if response == '1':
        pass
    elif response == '2':
        pass
    elif response == '3':
        pass
    elif response == '4':
        pass
    else:
        print('Invalid Option!')

checker()

# Neil's Account Test Data
neil = Account(1.00, 'Neil', 50.00)

# John's Account Test Data
john = Account(100.00, 'John', 1.00)

# Sue's Account Test Data
Sue = Account(50.00, 'Sue', 50.00)

关键修改点

  • 将用户输入的PIN通过int()转换为整数,和pin_no列表中的元素类型统一
  • 增加try-except捕获非数字输入,避免程序崩溃
  • 修正重试次数的计算逻辑,确保剩余次数提示正确
  • 登录成功或尝试耗尽后设置end=True,终止循环

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.13 12:41:16