当密码具有所有必需的前提时,为什么我的代码不打印密码?

时间:2019-10-05 13:09:45

标签: python-3.x passwords

`f=0
import random
chars_lower= "abcdefghijklmnopqrstuvwxyz"
chars_upper= "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
numbers= "0123456789"
symbols= "¡!¿?<>$#@%&€*+-"
password_length= 8
chars= "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789¡!¿?<>$#@%&€*+-"
while f==0:
    password_random= "".join(random.sample(chars, password_length))
    e= 0
    for i in chars_lower:
        for j in chars_upper:
            for n in numbers:
                for k in symbols:
                    if i in password_random and j in password_random and k in password_random and n in password_random:
                        e=1
                        f=1
if e==1:
    print(password_random)`

创建自动密码,以防用户在3次尝试后未正确输入密码

t=0
list_lower=[]
list_upper=[]
list_numbers=[]
list_symbols=[]
for i in chars_lower:
    list_lower.append(i)
for j in chars_upper:
    list_upper.append(j)
for n in numbers:
    list_numbers.append(n)
for k in symbols:
    list_symbols.append(k)
for i in range(3):
    name= input("Write a name that contains 8 characters and hit ENTER: ")
    password= input("Write a password that contains 8 characters and hit ENTER: ")
    if len(name)==8 and len(password)==8: 
        for i in password:
            if i in list_lower:
                for j in password:
                    if j in list_upper:
                        for n in password:
                            if n in list_numbers:
                                for k in password:
                                    if k in list_symbols:
                                        t=1
                                    else:
                                        t=2
                            else:
                                t=2
                    else:
                        t=2
            else:
                t=2
    else:
        t=2
    if t==1:
        break

if t==1:
    print("Your user name is",name,"and your password is",password)
elif t==2:
    print("You have already used all your chances to write your password, so your automatic password is:",password_random)

此代码要求用户输入包含8个字符的用户名和包含8个字符以及至少一个大写,一个小写,一个数字和一个符号的密码。如果满足所有这些前提条件,则应该打印用户名和密码。但是,事实并非如此。

1 个答案:

答案 0 :(得分:0)

如前所述,即使满足if条件,您也不会脱离“密码检查”内部的任何循环。因此,每个循环将遍历密码中的所有字符。尝试在代码中放置一些print语句以查看发生了什么。通常,嵌套如此多的循环和条件几乎变得难以读取。因此,我建议您看一下Python的anyall

使用这些,您可以像这样重构您的密码检查

def check_pw(pw):
    chars = "abcdefghijklmnopqrstuvwxyz"
    numbers = "0123456789"
    symbols = "¡!¿?<>$#@%&€*+-"
    contains_lower = any(l in pw for l in chars)
    contains_upper = any(l in pw for l in chars.upper())
    contains_nbr = any(l in pw for l in numbers)
    contains_sym = any(l in pw for l in symbols)
    return all((contains_lower, contains_upper, contains_nbr, contains_sym))

测试:

pws = ['asdfS7!l', 'asdf', 'TESTASEDFASDF']
for passphrase in pws:
    if len(passphrase) >= 8 and check_pw(passphrase):
        print(f"pw '{passphrase}' is ok.")
    else:
        print(f"pw '{passphrase}' is not ok.")

打印

pw 'asdfS7!l' is ok.
pw 'asdf' is not ok.
pw 'TESTASEDFASDF' is not ok.