首页 > 解决方案 > Python没有遍历列表中的项目

问题描述

我正在尝试制作一个提供密码然后测试密码强度的程序。但是,该程序无法按预期工作。在我添加强度测试功能之前,密码的初始生成工作正常,除了强度测试功能之外,仍然可以。该check(passw)功能在单独使用时也可以正常工作,只是当我将其放入for item in passwords_list 循环中时,它会打印:

Currently testing: 

Length under 3 characters.
Could not find a number.
Could not find a symbol.
No uppercase characters were found.
No lowercase characters were found.

代码如下:

import random
import string
adjectives = ['Hairy', 'Skinny', 'Red', 'Flawless']
nouns = ['Rock', 'Table', 'Banana', 'House']
passwords_list = ['']
password = ''

def check(passw):
    safe_unsafe = ['']
    errors_list = ['']
    errors = ''
    if len(passw) < 3:
        errors_list.append('length_under')
        errors = errors + '\nLength under 3 characters.'
    if len(passw) > 16:
        errors_list.append('length_above')
        errors = errors + '\nLength above 16 characters.'
    if not re.search('[0-9]', passw):
        errors_list.append('numbers')
        errors = errors + '\nCould not find a number.'
    for char in passw:
        if char in string.punctuation:
            safe_unsafe.append('y')
        else:
            safe_unsafe.append('n')
    if 'y' in safe_unsafe:
        pass
    else:
        errors_list.append('symbol')
        errors = errors + '\nCould not find a symbol..'
    safe_unsafe.clear()
    for char in passw:
        if char in string.ascii_uppercase:
            safe_unsafe.append('y')
        else:
            safe_unsafe.append('n')
    if 'y' in safe_unsafe:
        pass
    else:
        errors_list.append('upper')
        errors = errors + '\nNo uppercase characters were found.'
    safe_unsafe.clear()
    for char in passw:
        if char in string.ascii_lowercase:
            safe_unsafe.append('y')
        else:
            safe_unsafe.append('n')
    if 'y' in safe_unsafe:
        pass
    else:
        errors_list.append('lower')
        errors = errors + '\nNo lowercase characters were found.'
    safe_unsafe.clear()
    if errors:
        print(errors)
    else:
        print('Your password is all clear!')

    other_t = input('Would you like to test another password? y/n ')
    if other_t == 'y':
        password = str(input('What is your password? '))
        check(password)
    else:
        exit()

num_of_pass = input('How many passwords would you like?')
numbers = input('Would you like numbers? y/n').lower()
symbols = input('Would you like symbols? y/n').lower()
for num in range(int(num_of_pass)):
    password = password + random.choice(adjectives) + random.choice(nouns)
    if numbers == 'y':
        password = password + str(random.randint(1, 100))
    if symbols == 'y':
        password = password + str(random.choice(string.punctuation))
    passwords_list.append(password)
    print(password)
# test the password
for item in passwords_list:
    print('Currently testing ' + item)
    check(item)

标签: python

解决方案


在对您的代码进行了一些逆向工程和测试之后,我想我已经得出了一个结论。

在第七行,您分配password_list了一个空字符串列表。因此,您检查的第一个密码实际上是一个空字符串。

要解决此问题,password_list = ['']请使用password_list = []. 进行此修复后,我能够使您的代码运行。


推荐阅读