Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/343.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 登录认证系统_Python_Python 3.x_Csv_For Loop_Input - Fatal编程技术网

Python 登录认证系统

Python 登录认证系统,python,python-3.x,csv,for-loop,input,Python,Python 3.x,Csv,For Loop,Input,我正在为我提议的一个学校项目制作一个python登录系统,我有一个我找不到的逻辑错误。该程序应允许三次登录尝试,并在您登录时显示“correct”,但当您输入正确的用户名和密码时,该程序将运行,并打印“您输入了错误的密码”并循环该程序,然后当您输入任何值时,它将停止该程序,而不会显示任何内容。你能帮我找出逻辑错误吗。 我很乐意解释任何功能的作用:) 这里的逻辑有点缺陷,我已经稍微纠正了一下——不过你的思路是对的 首先,您要求用户在while循环之外进行输入。这意味着,每次您循环返回时,用户都没有

我正在为我提议的一个学校项目制作一个python登录系统,我有一个我找不到的逻辑错误。该程序应允许三次登录尝试,并在您登录时显示“correct”,但当您输入正确的用户名和密码时,该程序将运行,并打印“您输入了错误的密码”并循环该程序,然后当您输入任何值时,它将停止该程序,而不会显示任何内容。你能帮我找出逻辑错误吗。 我很乐意解释任何功能的作用:)


这里的逻辑有点缺陷,我已经稍微纠正了一下——不过你的思路是对的

首先,您要求用户在
while
循环之外进行输入。这意味着,每次您循环返回时,用户都没有机会输入新的详细信息

其次,使用
elif
语句,每当用户/通行证与
login
列表中的一行不匹配时,就会运行该语句。我们只在浏览了整个文件并且没有找到匹配项后运行此代码来修复此问题

第三,如果没有匹配项,则不要循环回while循环的开头,而是使用
logging()
再次调用函数。这将启动函数的新迭代,由于它永远不会返回任何内容,您将陷入无限递归。相反,我们循环回到while函数的开头

到达尝试3之后,我添加了一条消息和一个返回值
logging()
现在返回
True
,如果用户在3次尝试中输入了正确的用户/密码组合,则返回
False

下面是示例代码

def logging():
    # Set attempt number to 0
    atmptcount = 0

    # Keep asking until max number of attempts is reached
    while atmptcount <= 3:

        # Prompt user for input
        usr = input('Please enter your username: ')
        pas = input('Please enter your password: ')

        # Check through login file
        for line in login:
            log = line.split(',')

            # If details are correct, tell the user and return True
            if usr == log[0] and pas == log[1]:
                print('correct')
                return True

        # If we reached the end of the login file and nothing matched, increase
        # attempts by 1 and inform the user.
        atmptcount = atmptcount + 1
        print('Sorry you have entered your details incorrectly, please try again')

    # If the user has reached the max attempts, inform them and return False
    print('Reached max number of attempts')
    return False
logging()
def日志记录():
#将尝试次数设置为0
atmptcount=0
#继续询问,直到达到最大尝试次数

atmptcount时,请尝试打印出
log[0]
log[1]
,然后查看值是什么
def logging():
    # Set attempt number to 0
    atmptcount = 0

    # Keep asking until max number of attempts is reached
    while atmptcount <= 3:

        # Prompt user for input
        usr = input('Please enter your username: ')
        pas = input('Please enter your password: ')

        # Check through login file
        for line in login:
            log = line.split(',')

            # If details are correct, tell the user and return True
            if usr == log[0] and pas == log[1]:
                print('correct')
                return True

        # If we reached the end of the login file and nothing matched, increase
        # attempts by 1 and inform the user.
        atmptcount = atmptcount + 1
        print('Sorry you have entered your details incorrectly, please try again')

    # If the user has reached the max attempts, inform them and return False
    print('Reached max number of attempts')
    return False
logging()