Hướng dẫn how to verify a username and password in python - cách xác minh tên người dùng và mật khẩu trong python

Tôi cần tạo một chương trình trong đó một lần tôi nhập tên người dùng và mật khẩu, nó sẽ xác minh đầu vào bằng cách hỏi lại tên người dùng và mật khẩu của tôi và khiến tôi lặp lại nếu nó không khớp. Tôi bị mắc kẹt trên mã bên dưới và không biết làm thế nào để sửa nó. Cứu giúp!

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")

Hướng dẫn how to verify a username and password in python - cách xác minh tên người dùng và mật khẩu trong python

da

12.6K4 Huy hiệu vàng45 Huy hiệu bạc87 Huy hiệu đồng4 gold badges45 silver badges87 bronze badges

Đã hỏi ngày 16 tháng 3 năm 2017 lúc 10:27Mar 16, 2017 at 10:27

1

user của bạn không được lưu trữ theo cách tốt nhất có thể, hãy thử sử dụng một dict. Thay vào đó, bạn có thể thử một cái gì đó như thế này: (đã sửa một số sai lầm và cải tiến)

# 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")

Đã trả lời ngày 16 tháng 3 năm 2017 lúc 10:43Mar 16, 2017 at 10:43

TakutakuTaku

29,9k11 Huy hiệu vàng69 Huy hiệu bạc81 Huy hiệu Đồng11 gold badges69 silver badges81 bronze badges

n chỉ được xác định trong hàm Access(), trong vòng lặp trong khi của bạn, chương trình sẽ không biết n là gì.

Trong phần trong khi, hãy thử ____10 và

# 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

Đã trả lời ngày 16 tháng 3 năm 2017 lúc 10:40Mar 16, 2017 at 10:40

Mã phải như thế này, ________ 7 không được xác định trong vòng lặp

# 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")
3:

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

Nhân tiện, tôi khuyên bạn nên sử dụng cấu trúc

# 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")
4:

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")

Đã trả lời ngày 16 tháng 3 năm 2017 lúc 10:33Mar 16, 2017 at 10:33

McGradymcgradyMcGrady

10.2K13 Huy hiệu vàng47 Huy hiệu bạc65 Huy hiệu Đồng13 gold badges47 silver badges65 bronze badges

Bạn đang ở trong một vòng lặp vô hạn vì hoàn thành không bao giờ được đặt thành đúng. Futer bạn muốn khớp với tên người dùng và sau đó là mật khẩu. Tôi đã làm nó để bạn có thể có một cơ sở dữ liệu với tên và mật khẩu và so sánh nó với đầu vào mới. Tất nhiên bạn cũng có thể sử dụng nó chỉ với một tên người dùng và mật khẩu. Hy vọng nó cho một số ý tưởng.

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!")

Đã trả lời ngày 16 tháng 3 năm 2017 lúc 10:36Mar 16, 2017 at 10:36

J. GOEDHARTJ. GOEDHARTJ. Goedhart

2091 Huy hiệu bạc14 Huy hiệu đồng1 silver badge14 bronze badges

 https://github.com/soumilshah2995/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__ = ""
__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)

Đã trả lời ngày 21 tháng 6 năm 2019 lúc 3:13Jun 21, 2019 at 3:13

Hướng dẫn how to verify a username and password in python - cách xác minh tên người dùng và mật khẩu trong python

Làm thế nào để Python xác nhận email và mật khẩu?

Khoa học dữ liệu thực tế sử dụng Python..
Định dạng phải là [email protected] định dạng ..
Tên người dùng chỉ có thể chứa các chữ cái, số, số, dấu gạch ngang và chữ dưới ..
Tên công ty chỉ có thể chứa các chữ cái và số chữ thường ..
Tên miền chỉ có thể chứa các chữ cái trên và chữ thường ..

Làm thế nào để Python lưu trữ tên người dùng và mật khẩu?

Bạn có thể lưu trữ tên người dùng/mật khẩu trên hai dòng đầu tiên của tệp văn bản đơn giản, sau đó sử dụng Python để đọc nó khi bạn cần.Nếu tệp văn bản nằm trong thư mục kho lưu trữ, bạn nên sửa đổi.Gitignore để đảm bảo nó không được theo dõi bởi điều khiển nguồn.on the first two lines of a plain text file, then use python to read it when you need it. If the text file is in the repository directory you should modify . gitignore to ensure it's not tracked by source source control.