Функция не будет перебирать строки как обычно - PullRequest
0 голосов
/ 05 ноября 2019

Я работаю над проблемой домашнего задания, которая попросила меня написать систему проверки имени пользователя и пароля. Я выполнил большую часть работы и сейчас нахожусь на этапе отладки. Я столкнулся с очень специфической проблемой, заключающейся в том, что я не могу перебирать строки в своей парольной части программы. Как ни странно, моя часть имени пользователя программы использует то же самое для логики цикла, и она работает просто отлично. Но когда дело доходит до парольной части, он может повторять только первый символ моего ввода, и итерация на этом останавливается (проверяется печатью символов в цикле for). Это часть моего пароля, которая не повторяется:

def length_pass(password):
    print("* Length of password:", len(password))
    if len(password) < 8:
        return False
    else:
        return True

def find_username(password):
    if username in password:
        print("* Username is part of password True")
        return True
    else:
        print("Username is part of password False")
        return False

def U_pass(password):
    y = 0
    for character in password:
        if character.isupper():
            y += 1
        else:
            y += 0
        if y >= 1:
            print("* # of uppercase characters in the password:", y)
            return True
        else:
            print("* # of uppercase characters in the password: 0")
            return False

def L_pass(password):
    y = 0
    for character in password:
        if character.islower():
            y += 1
        else:
            y += 0
        if y >= 1:
            print("* # of lowercase characters in the password:", y)
            return True
        else:
            print("* # of lowercase characters in the password: 0")
            return False

def num_pass(password):
    y = 0
    for character in password:
        if character.isdigit():
            y += 1
        else:
            y += 0
        if y >= 1:
            print("* # of numbers in the password:", y)
            return True
        else:
            print("* # of numbers in the password: 0")
            return False

def poperation(password):
        length_pass(password)
        find_username(password)
        U_pass(password)
        L_pass(password)
        num_pass(password)


def main():
    Go = True
    while Go == True:
        password = input("Enter a password:")
        poperation(password)
        if (find_username(password) == False and length_pass(password) and U_pass(password)
        and L_pass(password) and num_pass(password)== True):
            print("Password is valid!")
            Go = False
            break
        else:
            print("Password is invalid, please try again")
username = "Weee1357T"
main()

вывод:

Enter a password:Hyperion123456
* Length of password: 14
Username is part of password False
* # of uppercase characters in the password: 1
* # of lowercase characters in the password: 0
* # of numbers in the password: 0
Username is part of password False
* Length of password: 14
* # of uppercase characters in the password: 1
* # of lowercase characters in the password: 0
#The output also got print twice which is very strange
Password is invalid, please try again

Вот часть моего имени пользователя программы:

def vaild_length(username):
    print("* Length of password:", len(username))
    if len(username) < 8 or len(username)>10:
       return False
    else:
        return True


def vaild_alnum(username):
    username.isalnum()
    print("* All characters are alpha-numeric:", username.isalnum())
    if username.isalnum() == True:
        return True
    else:
        return False

def first_and_last(username):
    username[0].isdigit()
    username[-1].isdigit()
    if username[0].isdigit() or username[-1].isdigit() == True:
        print("* First & last characters are not digits: False")
        return False
    else:
            print("* First & last characters are not digits: True")
            return True

def upper(username):
    x = 0
    for character in username:
        if character.isupper():
            x += 1
        else:
            x += 0

    if x >= 1:
        print("* # of uppercase characters in the username:", x)
        return True
    else:
        print("* # of uppercase characters in the username:", x)
        return False


def lower(username):
    x = 0
    for character in username:
        if character.islower():
            x += 1
        else:
            x += 0
    if x >= 1:
        print("* # of lowercase characters in the username:", x)
        return True
    else:
        print("* # of lowercase characters in the username:", x)
        return False


def num(username):
    x = 0
    for character in username:
        if character.isdigit():
            x += 1
        else:
            x += 0
    if x >= 1:
        print("* # of numbers characters in the username:", x)
        return True
    else:
        print("* # of numbers characters in the username:", x)
        return False

def main1():
    Go = True
    while Go == True:
        global username
        username = input("Enter a Username:")
        vaild_length(username)
        vaild_alnum(username)
        first_and_last(username)
        upper(username)
        lower(username)
        num(username)

        if (vaild_length(username) and vaild_alnum(username) and first_and_last(username) and
        upper(username) and lower(username) and num(username) == True):
            print("Username is valid!")
            Go = False
            break
        else:
            print("Username is invalid, please try again")

main1()

output:

Enter a Username:Hyperion1234T
* Length of password: 13
* All characters are alpha-numeric: True
* First & last characters are not digits: True
* # of uppercase characters in the username: 2
* # of lowercase characters in the username: 7
* # of numbers characters in the username: 4
* Length of password: 13
Username is invalid, please try again

Как видно, часть имени пользователя программы работает идеально, но то же самое для конструкций цикла не работает для части пароля. Я не могу понять, почему это произошло, потому что та же логика прекрасно работала 50 строк назад.

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...