希望你有一个伟大的一天。我创建了一个输入密码界面。输入正确的密码后,它应该显示在标签中授予的访问权限,这与我的代码很好。然而,在不正确的密码的情况下,我想只包括3次尝试。如果你输入不正确的密码第一次和第二次,它应该显示“不正确的密码”。在最后一次尝试后,它应该显示“帐户被阻止”.问题是,当涉及到不正确的密码和第一次尝试,它直接显示“帐户被阻止”这是我到目前为止,
from tkinter import *
root = Tk()
root.geometry("300x300")
# Creating Button Command
def password():
correct_password = "123"
enter_the_password = pass_entry.get()
number_of_try=0
number_of_max_try=3
max_try=""
while enter_the_password!=correct_password and max_try!="reached":
if number_of_try<number_of_max_try:
enter_the_password
number_of_try=number_of_try+1
if enter_the_password!=correct_password:
response= "Incorrect Password"
else:
max_try="reached"
if max_try=="reached":
response= "Too many attempts. Account blocked"
else:
response= "Access Granted"
pass_label.config(text=response)
pass_entry.delete(0, END)
# Creating widget
pass_entry=Entry(root, width=30)
pass_entry.pack(pady=5)
done_button= Button(root, text="Press", command=password).pack(pady=10)
pass_label= Label(root, width=30)
pass_label.pack(pady=10)
root.mainloop()
字符串
1条答案
按热度按时间g6ll5ycj1#
您的
while
循环立即重新进入,而不允许用户输入另一个密码。实际上,您不需要该循环,因为root.mainloop()
已经提供了它。相反,你应该删除你的
while
循环,并在你的函数password
之外初始化你的变量。每次按下done_button
,函数password
应该只检查一次密码。