Python 将正确的结果传递出身份验证循环
我正在创建一个登录系统,它检索输入框中的详细信息,并将其与数据库中的详细信息进行比较。如果在数据库中找到输入的详细信息,则运行Python 将正确的结果传递出身份验证循环,python,loops,Python,Loops,我正在创建一个登录系统,它检索输入框中的详细信息,并将其与数据库中的详细信息进行比较。如果在数据库中找到输入的详细信息,则运行Bottom()函数。如果未找到详细信息,请用户重试 目前,程序循环直到找到它。但是,由于我设置了else语句,如果数据库中的第一项不是输入的详细信息,则else部分仍将运行。是否有办法更改此值,以便else是else和数据库中的最后一个值? 以下是函数: #Retrieves the information entered into entry box def Get_
Bottom()
函数。如果未找到详细信息,请用户重试
目前,程序循环直到找到它。但是,由于我设置了else语句,如果数据库中的第一项不是输入的详细信息,则else
部分仍将运行。是否有办法更改此值,以便else
是else
和数据库中的最后一个值?
以下是函数:
#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
请注意,else已被移回与for循环成对的位置这很有希望按您所希望的方式工作,现在它在行上循环,并根据
行[0]
和行[1]
检查用户名/密码。如果找到匹配项,它将中断并不会执行连接到for循环的else
此外,我还删除了for循环over行
,因为无论如何都没有使用字段
变量
#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")
似乎您想要
elif
而不是else
。是的,但是elif是什么呢?在提供正确的凭据之前让函数本身循环是没有意义的,然后才运行Bottom()
?此外,每次重新读取密码文件似乎效率很低。
#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")