Python、用户名和密码,尝试 3 次

7UN41D

我大约 2 周前开始学习 python,并正在尝试创建一个可以存储多个用户名和密码的密码系统。我正在尝试添加一些代码,如果您输入错误密码 3 次,它会让您等待一分钟。

我不断收到错误消息,如果您输入密码错误 3 次并等待 60 秒,即使您输入了正确的密码和用户名,它也会打印“您的密码错误。您只剩下 3 次尝试了。”

有人可以帮助并指出我在代码中的缺陷吗?

import time
username = ["Test","Test2"]                 
password = ["1Ac!","2Bd!"]
success = 0
Tries = 0
Valid = False                                            
Pass = ""
    
            
def login():                                                    
    global success
    global Pass                                                 
    global Tries
    inuser = input(str("Enter your username: \n"))
    inpass = input(str("Enter your password: \n"))
    if inuser not in username:
        print("username not found in directory")
        login()                                                   
    else:
        posUser = username.index(inuser)
        Pass = password[posUser]                                 
        while success != 1:
            if Tries == 3:
                print("You have had too many attempts. Wait 60 seconds before trying again")
                for i in range(60, 0, -1):
                    time.sleep(1)
                    print(i)
            if Tries == 3:
                Tries = 0
                inuser=str("0")
                inuser=str("0")
                login()
            else:
                break
    if Pass == inpass:
        success=1
    else:
        Tries += 1
        AttemptsLeft = (3 - Tries)+ 1 
        print("Your password is wrong. You have only {} attempts left.".format(AttemptsLeft))
        login()
        

        
login()
if success==1:
    print("Welcome to ------")
达斯朱利安

如果你摆脱递归,一切都会变得更简单。

import time
username = ["Test","Test2"]                 
password = ["1Ac!","2Bd!"]
success = 0
Tries = 0
Valid = False                                            
Pass = ""
    
            
def login():                                                    
    global success
    global Pass                                                 
    global Tries                                     
                            
    while success != 1:
        inuser = input(str("Enter your username: \n"))
        inpass = input(str("Enter your password: \n"))
        if inuser not in username:
            print("username not found in directory")
        else:
            posUser = username.index(inuser)
            Pass = password[posUser]         
        if Tries == 3:
            print("You have had too many attempts. Wait 60 seconds before trying again")
            for i in range(60, 0, -1):
                time.sleep(1)
                print(i)
            Tries = 0
            inuser=str("0")
            inuser=str("0")
        if Pass == inpass:
            success=1
        elif Pass != "":
            Tries += 1
            AttemptsLeft = (3 - Tries)+ 1 
            print("Your password is wrong. You have only {} attempts left.".format(AttemptsLeft))
        

        
login()
if success==1:
    print("Welcome to ------")

您还应该考虑摆脱global变量。相反,使用return.

import time
username = ["Test","Test2"]                 
password = ["1Ac!","2Bd!"]
success = 0
Tries = 0
Valid = False                                            
Pass = ""
    
            
def login():                                                    
    success = 0
    Pass = ""                                          
    Tries = 0                                  
                            
    while success != 1:
        inuser = input(str("Enter your username: \n"))
        inpass = input(str("Enter your password: \n"))
        if inuser not in username:
            print("username not found in directory")
        else:
            posUser = username.index(inuser)
            Pass = password[posUser]         
        if Tries == 3:
            print("You have had too many attempts. Wait 60 seconds before trying again")
            for i in range(10, 0, -1):
                time.sleep(1)
                print(i)
            Tries = 0
            inuser=str("0")
            inuser=str("0")
        if Pass == inpass:
            return True
        elif Pass != "":
            Tries += 1
            AttemptsLeft = (3 - Tries)+ 1 
            print("Your password is wrong. You have only {} attempts left.".format(AttemptsLeft))
        

        

if login():
    print("Welcome to ------")

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

简单的用户名和密码登录 Python 3

python3以安全的方式检查ldap用户名和密码

python3-在.csv中存储用户名和哈希密码

为什么我必须一次又一次在TYPO3的Pluginsettings中输入用户名/密码(.htaccess / .htpasswd)?

使用JSch的Ssh到远程机器,询问两次用户名和密码

要求用户在重置密码前至少尝试 3 次

如何使用2或3个用户名和密码以用户形式创建登录名?

sqlite3 和 tkinter 中的用户名和密码变量问题

从 sqlite3 填充组合框可编辑用户名和行编辑密码

如何实现密码的3次尝试授权?

为什么wget第一次不使用URL中的用户名和密码

Java要求用户输入密码3次

在ipad上运行应用程序一次又一次询问系统用户名和密码

有没有更好的方法一次在SQL中从3个用户名获取电子邮件?

我可以使用任何用户名和密码登录到Firebird 3数据库

尝试3次检查密码,密码自动生成

打印第三次尝试成功登录的用户名

为什么这个登录程序在我第一次正确输入用户名和密码时可以登录?

Linux-记录尝试的用户名和密码

python重复功能3次

使用python向用户询问用户名和密码

Spring Security 3-如何自定义用户名/密码参数?

Drive.API.v3不同的帐户用户名/密码身份验证

具有3个字段的Spring Security身份验证,而不仅仅是用户名和密码

使用Codeigniter限制登录尝试3次

将curl转换为python:用户名和密码

使用用户名和密码的Python github Pull

在python中使用硒填充用户名和密码

Python / Django数据库用户名和密码?