python - ATM卡密码验证-Python
问题描述
我正在使用 TKinter 和 Python 开发基于 UI 的 ATM 的项目,用户将输入卡密码,通过验证并移动到检查屏幕,这工作正常。现在我需要将消息显示为“Invalid PIN number 1 to 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")
解决方案
首先,Text
每次要显示一些消息时,您都在更改小部件的文本。在您接受输入的同一位置显示输出不是一个非常正确的想法。
我建议使用 aLabel
来显示消息,或者也可以使用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()
推荐阅读
- c++ - Mac Mojave 上的 OpenCV C++ Xcode 错误 NSCameraUsageDescription
- node.js - Docker:从容器创建后执行的命令中分离
- node.js - 我存储个人资料图片的路径在生产模式下无法访问
- django - 从分页中排除一种类型的帖子
- java - 让用户猜测一个随机数并循环,直到他们猜对为止
- typescript - 未找到修改后的第三方包的模块
- node.js - 使用请求模块 HTTP GET Firebase 函数 Node.js
- python - 如何将字符串与bs4对象进行比较
- fortran - 不同长度的Fortran子串比较问题
- entity-framework - 如何从现有的 sql server DB 生成模型类