简体   繁体   中英

Python not iterating through items in a list

I'm trying to make a program that gives a password and then tests the strength of a password. However, this program does not work as expected. The initial generating of the password worked fine before I added the strength testing feature, and other than the strength testing feature, still does. The check(passw) function also works fine when on its own, it's just when I put it in the for item in passwords_list loop, it prints:

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.

The code is as follows:

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)

After some reverse engineering and testing of your code, I think I have came to a conclusion.

On line seven, you assigned password_list to a list with an empty string. Because of this, the first password that you checked, is actually an empty string.

To fix this, make password_list = [''] to password_list = [] . After making this fix, I was able to make your code run.

You are not creating an epty list. So when you check the first item on list it checks an empty string, which don't pass the test.

Then you end your check function calling it again, before going to the next item in the for loop.

You have to start with an epty list so simply change passwords_list = [''] to passwords_list = []

And remove the test another password from inside the check function to keep the for loop running.

First you are creating a list with empty string

password_list = [''] #is wrong

password_list = [] # is correct

Second Your code is not welsuited for working on a list of items. You need to remove the last 6 lines of your code to make it work perfectly.

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

These lines should be removed

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM