我正在创建一个登录系统,该系统检索输入框中的详细信息并将其与数据库的详细信息进行比较。如果在数据库中找到输入的详细信息,Bottom()
则运行该功能。如果未找到详细信息,则要求用户再试一次。
目前,程序循环直到找到它。但是,因为我已经设置了else语句,如果数据库中的第一项不是输入的详细信息,该else
部分仍将运行。有没有一种方法,我可以改变这种做法,else
是else
和 last value in the database?
这是函数:
#Retrieves the information entered into entry box
def Get_Details():
user_namev2=user_name.get().lower() #Make it so entry box goes red if passwords password is incorrect, and red if username is incorrect/not fault
user_passwordv2=user_password.get().lower()
with open ('Accounts.csv', 'r') as Account_file:
reader = csv.reader(Account_file)
for row in reader:
for field in row:
if row[0] == user_namev2 and row[1] == user_passwordv2:
Bottom()
break
else:
nomatch_label = Label(canvas, width = 40, height = 2, text = "USERNAME AND PASSWORD NOT FOUND", fg = "red", bg = "gray21", font = font2)
canvas_nomatch_label = canvas.create_window(500, 550, window = nomatch_label)
name_entry.config(fg = "red")
password_entry.config(fg="red")
break
发布于 2019-01-07 10:31:42
#Retrieves the information entered into entry box
def Get_Details():
user_namev2=user_name.get().lower() #Make it so entry box goes red if passwords password is incorrect, and red if username is incorrect/not fault
user_passwordv2=user_password.get().lower()
with open ('Accounts.csv', 'r') as Account_file:
reader = csv.reader(Account_file)
found = False
for row in reader:
for field in row:
if row[0] == user_namev2 and row[1] == user_passwordv2:
Bottom()
found = True
break
else:
if not found:
nomatch_label = Label(canvas, width = 40, height = 2, text = "USERNAME AND PASSWORD NOT FOUND", fg = "red", bg = "gray21", font = font2)
canvas_nomatch_label = canvas.create_window(500, 550, window = nomatch_label)
name_entry.config(fg = "red")
password_entry.config(fg="red")
break
请注意,else已经转移回与for循环配对
发布于 2019-01-07 11:28:12
这应该按照您的预期方式工作,现在它循环遍历行并检查用户名/密码row[0]
和row[1]
。如果找到匹配则会中断并且不会执行连接到for循环的else。
此外,我删除了列中的for-loop循环,因为它始终都没有使用变量field。
#Retrieves the information entered into entry box
def Get_Details():
user_namev2=user_name.get().lower() #Make it so entry box goes red if passwords password is incorrect, and red if username is incorrect/not fault
user_passwordv2=user_password.get().lower()
with open ('Accounts.csv', 'r') as Account_file:
reader = csv.reader(Account_file)
for row in reader:
if row[0] == user_namev2 and row[1] == user_passwordv2:
Bottom()
break
else:
nomatch_label = Label(canvas, width = 40, height = 2, text = "USERNAME AND PASSWORD NOT FOUND", fg = "red", bg = "gray21", font = font2)
canvas_nomatch_label = canvas.create_window(500, 550, window = nomatch_label)
name_entry.config(fg = "red")
password_entry.config(fg="red")
https://stackoverflow.com/questions/-100008952
复制相似问题