验证Python

时间:2017-03-16 10:27:35

标签: python

我需要制作一个程序,我曾经输入用户名和密码,它会通过再次询问我的用户名和密码验证输入,如果不匹配则让我重复它。我被困在下面的代码,不知道如何解决它。救命啊!

import time
complete = False
user = [["username",""],["password",""]]

def Access():
    for n in range (len(user)):
        user[n][1] = input(user[n][0])

while not complete:
    Access()
    username = input("What is the username?")
    password = input("What is the password?")

    if username == user[n][0]:
        print("Good!")
    else:
        print("Input username again!")
    if password == user[n][1]:
        print("User has been identified, Welcome",username)
    else:
        print("Input password again")

5 个答案:

答案 0 :(得分:1)

您的user未以最佳方式存储,请尝试使用dict。你可以尝试这样的事情:(修正一些错误并做出改进)

# I believe this is what you're trying to do
complete = False
user = {"some username" : "some password", "more username" : "more password"}

while not complete:
    username = input("What is the username?")
    password = input("What is the password?")
    conf_username = input("Repeat the username?")
    conf_password = input("Repeat the password?")
    # since in your question you said you wanted to ask the user to repeat
    if username != conf_username or password != conf_password:
        print("username or password does not match") # print a message if different inputs
        continue # restarts
    if not username in user: # check to see if user does not exists
        print("Input username again!")
        continue
    if password == user[username]: # check to see if password match
        print("User has been identified, Welcome",username)
        complete = True
    else:
        print("Input password again")

答案 1 :(得分:0)

代码应如下所示,n循环中未定义while

if username == user[0][0]:
    print("Good!")
else:
    print("Input username again!")
if password == user[1][1]:
    print("User has been identified, Welcome", username)
    complete = True

顺便说一句,我建议您使用dictionary结构:

user_pass = {}

while True:
    user = input("Your name")
    pwd = input("Your password")

    if user in user_pass and pwd == user_pass[user]:
        print("Welcome", user)
        break
    else:
        user_pass[user]=pwd
        print("registration completed,please login")

答案 2 :(得分:0)

您处于无限循环中,因为complete永远不会设置为true。您想要匹配用户名和密码。我做了它,所以你可以拥有一个包含名称和密码的数据库,并将其与新输入进行比较。您当然也可以只使用一个用户名和密码来使用它。希望它能给出一些想法。

import time
complete = False
user = [["username","password"],["username2","password2"]]

while not complete:
    username = input("What is the username?")
    password = input("What is the password?")
    for n in len(user):
         if username == user[n][0]:
              print("Good!")
              if password == user[n][1]:
                   print("User has been identified, Welcome",username)
                   complete = True
              else:
                   break
                   print("Input password again")
    if not complete:
        print("Input username again!")

答案 3 :(得分:0)

n仅在Access()函数中定义,在您的while循环中,程序将不知道n是什么。

在while部分,尝试if username == user[0][0]if password == user[1][1]

答案 4 :(得分:-1)

 https://github.com/soumilshah1995/UserName-and-Password-Validation-Python  

#!/usr/bin/env python3
__author__ = "Soumil Nitin Shah"
__copyright__ = "Copyright 2007, The Cogent Project"
__credits__ = ["Rob Knight"]
__license__ = "GPL"
__version__ = "1.0.1"
__maintainer__ = "Soumilshah"
__email__ = "soushah@my.bridgeport.edu"
__status__ = "Testing"
from flask_bcrypt import Bcrypt



class Authentication(object):

def __init__(self, username = ''):
    self.username = username

def __lower(self):
    lower = any(c.islower() for c in self.username)
    return lower

def __upper(self):
    upper = any(c.isupper() for c in self.username)
    return upper

def __digit(self):
    digit = any(c.isdigit() for c in  self.username)
    return digit

def validate(self):
    lower = self.__lower()
    upper = self.__upper()
    digit = self.__digit()

    length = len(self.username)

    report =  lower and upper and digit and length >= 6

    if report:
        print("Username passed all checks ")
        return True

    elif not lower:
        print("You didnt use Lower case letter")
        return False

    elif not upper:
        print("You didnt userUpper case letter")
        return False

    elif length <6:
        print("username should Atleast have 6 character")
        return False

    elif not digit:
        print("You didnt use Digit")
        return False
    else:
        pass
enter username = "Youtu1221"
password = "SuperSecret123"

C = Authentication(username=username)
data = C.validate()

bcrypt = Bcrypt()


if (data):
  hash = bcrypt.generate_password_hash(username)
  print(hash)
else:
  pass

check = bcrypt.check_password_hash(hash, "Youtudd1221")
print(check)