首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >将正确的结果传递出认证循环

将正确的结果传递出认证循环
EN

Stack Overflow用户
提问于 2019-01-07 01:49:11
回答 2查看 0关注 0票数 0

我正在创建一个登录系统,该系统检索输入框中的详细信息并将其与数据库的详细信息进行比较。如果在数据库中找到输入的详细信息,Bottom()则运行该功能。如果未找到详细信息,则要求用户再试一次。

目前,程序循环直到找到它。但是,因为我已经设置了else语句,如果数据库中的第一项不是输入的详细信息,该else部分仍将运行。有没有一种方法,我可以改变这种做法,elseelse last value in the database?

这是函数:

代码语言:javascript
复制
#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
EN

回答 2

Stack Overflow用户

发布于 2019-01-07 10:31:42

代码语言:javascript
复制
#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循环配对

票数 0
EN

Stack Overflow用户

发布于 2019-01-07 11:28:12

这应该按照您的预期方式工作,现在它循环遍历行并检查用户名/密码row[0]row[1]。如果找到匹配则会中断并且不会执行连接到for循环的else。

此外,我删除了列中的for-loop循环,因为它始终都没有使用变量field。

代码语言:javascript
复制
#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")
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/-100008952

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档