ATM卡Pin验证

2024-06-25 05:44:48 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在做一个项目,使用TKinter和Python开发基于用户界面的ATM,用户将输入卡的pin码,通过验证并移动到检查屏幕,工作正常。现在,我需要将消息显示为“无效PIN码1到3”,第三次尝试后,我需要将消息显示为“多次尝试,您的卡被阻止,请联系客户服务。”

    def enter_pin():

        pinnum=self.txtRecipt.get("1.0","end-1c")
        if ((pinnum == str("0000")) or (pinnum == str("9874")) or (pinnum == str("5689") )):
            self.textRep.delete("1.0",END)
            self.textRep.insert(END, 'Welcome to MY ATM'  + "\n\n")
            self.textRecipt.insert(END, 'Withdraw Cash\n")
            self.textRecipt.insert(END, 'Print Receipt\n")
            self.textRecipt.insert(END, 'Balance\n")
            self.textRecipt.insert(END, 'Change Pin\n")
        else:
            self.txtRecipt.delete("1.0", END)
            self.txtRecipt.insert(END, 'Invalid Pin Number'+ "\n\n") 

现在,如何在上面的代码中添加下面的函数,以便它将显示在我的ATM UI的文本框中

def pin_num():
    attempt= 0
    while attempt < 3:
        num = input('Please Enter Your 4 Digit Pin: ')
        if enter_pin(pinnum):
            print("Pin accepted!")
        else:
            print("Invalid pin")
            attempt += 1
            print("Multiple attempts, Your card is blocked, please contact customer service")
    

Tags: self消息defpinendinsertprintenter
1条回答
网友
1楼 · 发布于 2024-06-25 05:44:48

首先,每次要显示消息时,都要更改Text小部件的文本。在接收输入的同一位置显示输出不是一个非常正确的想法

我建议使用Label来显示消息,或者也可以使用messagebox来显示应用程序中的消息框


但是,使用当前代码,您可以做的是为attempts创建一个全局变量,并根据您的逻辑编辑函数enter_pin

def enter_pin():   
    message = textRep.get("1.0","end-1c")
    if message=="Welcome\nYou are authorized!\n":        #if user already entered the correct pin, no need to check further
        return

    global attempts                            #the number of attempts left
    attempts -=1
    if attempts<0:                             #when no attempts left
        txtRecipt.delete("1.0", END)
        txtRecipt.insert(END, "Multiple attempts, Your card is blocked, please contact customer service")
        return                                 #no need to check further
    
    pinnum  = txtRecipt.get("1.0","end-1c")
    if ((pinnum == str("0000")) or (pinnum == str("9874")) or (pinnum == str("5689") )):
        textRep.delete("1.0", END)
        textRep.insert(END, 'Welcome\nYou are authorized!\n')
        #....whatever messages you want to display
    elif attempts==0:                          #if it's wrong pin and also this was the last try
        enter_pin()                            #then call this function again, then it will go to the case- if attempts<0
    else:
        txtRecipt.delete("1.0", END)
        txtRecipt.insert(END, 'Invalid Pin Number'+ "\n\n")

attempts = 3        #initialize the variable it with value 
txtRecipt = Text(root, height=2)
txtRecipt.pack()
b= Button(root, text="ENTER", command=enter_pin, width = 5)
b.config(font=("Helvetica", 10, 'bold'))
b.pack(pady = 10)
textRep   = Text(root, height=2)
textRep.pack()

相关问题 更多 >