M
M
Magic Code2020-07-21 14:20:03
Python
Magic Code, 2020-07-21 14:20:03

Why does the infinite loop work?

def passwd(length: int, difficult: int) -> List:
    # импортируем модуль для генерации случайных чисел
    import random
    # создаем кортежи из букв, символов и цифр
    chars: tuple = ('a', 'b', 'c', 'd',
                    'e', 'f', 'g', 'h',
                    'i', 'j', 'k', 'l',
                    'm', 'n', 'o', 'p',
                    'q', 'r', 's', 't',
                    'u', 'v', 'w', 'x',
                    'y', 'z')
    symbols: tuple = ('@', '$', '?', '#', '&')
    nums: tuple = tuple(range(10))
    # создаем пароль в зависимости от уровня сложности
    parole = []
    if difficult == 1:
        while len(parole) != length:  # length - длина пароля
            i = random.randrange(len(chars) - 1)  # диапазон кортежа - 1
            parole.append(chars[i])
            parole.append(chars[i].upper())  # если убрать эту строку, то цикл работает верно
    return parole


print(passwd(3, 1))

I don't understand why this is happening.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
Sergey Tikhonov, 2020-07-21
@Panda_Code

If you add two characters at a time, you will never catch an odd password length.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question