Login information validation does not work

Asked

Viewed 663 times

0

Doubts are throughout the program

import string


alphabet = list(string.ascii_lowercase)

n = range(0, 10)
name = input('Digite seu nome de usuário:')


if name in n:
    while True:
        print('Apenas letras são permitidas na nomeação do usuário.')
        name = input('Digite seu nome de usuário:')
        #o loop só encerra quando apenas uma letra é digitada, mais que isso já não dá certo. 
        if name not in n:
            break
if name not in alphabet:
    while True:
        print('Apenas letras são permitidas na nomeação do usuário.')
        name = input('Digite seu nome de usuário:')
        if name in alphabet:
            break


password = input('Digite sua nova senha:')
l = range(8, 17)
o = [',', ',', '@', '#', '$', '%', '¨', '&', '*', '(', ')', '_', '+', ':', '}', '{', 'º', '|']#Tentei colocar manualmente todos os caracteres que não são permitidos em senhas, porque procurei e não achei uma forma de adicioná-los como fiz na variável do alfabeto.

if password == name:
    while True:
        print('!Informações!\n\nSua senha não pode ser igual ao seu nome de usuário.\n')
        password = input('Digite sua nova senha:')
        if password != name:
            break


elif len(password) not in l:
    while True:
        print('\n\nSua senha deve ter no mínimo 8 e no máximo 16 caracteres.\n')
        password = input('Digite sua nova senha:')
        if len(password) in l:
            break
elif password not in alphabet:
    while True:
        print('\n\nSua senha deve conter letras e números.\n')
        password = input('Digite sua nova senha:')
        if password in alphabet:
            break
elif password not in n:
    while True:
        print('\n\nSua senha deve conter letras e números.\n')
        password = input('Digite sua nova senha:')
        if password in n:
            break
#com alguns números e letras dão certo e o loop se encerra, mas com outros números e letras o loop permanece.


elif password in o:
    while True:
        print('\n\nApenas letras e números são permitidos.\n')
        password = input('Digite sua nova senha:')
        if password not in o:
            break


password_2 = input('Digite novamente sua senha:')


else:
    if password_2 != password:
        while True:
            print('Suas senhas não coincidem, tente novamente.')
            if password_2 == password:
                break

print('Usuário registrado com sucesso.')
  • Did the answer solve your question? Do you think you can accept it? See [tour] if you don’t know how you do it. This would help a lot to indicate that the solution was useful to you. You can also vote on any question or answer you find useful on the entire site.

1 answer

2

Some remarks that will put you on the right track:

  • Make sure the input is within the parameters, do not check if it is outside. It may sound silly, but just accept what can be accepted and keep the amount of things accepted in a track that you have control of.
  • Use standard language operators and functions that have been made available for the task as simply as possible. Then use larger and smaller to compare a track and use isalnum(), for example, to determine if it is in the range you want.
  • No use trying to check if a string is contained within a list of characters, will only be true if the string has only one character.
  • Ask for the die only once, if the person has to enter again it has to be in the same place of the code, ie only have 3 input() (and 3 variables) in this code. One of the reasons for the error is too complex logic. This code be much simpler and shorter, then you can find the error easier and even eliminate it alone.

Thus:

while True:
    name = input('Digite seu nome de usuário:')
    if not name.isalpha():
        print('Apenas letras são permitidas na nomeação do usuário.')
    else:
        break
while True:
    password = input('Digite sua nova senha:')
    if password == name:
        print('\n\nSua senha não pode ser igual ao seu nome de usuário.\n')
    elif len(password) < 8 or len(password) > 16:
        print('\n\nSua senha deve ter no mínimo 8 e no máximo 16 caracteres.\n')
    elif not (any(char.isdigit() for char in password) and any(char.isalpha() for char in password)):
        print('\n\nSua senha deve conter letras e números.\n')
    else:
        break
while True:
    password2 = input('Digite sua nova senha:')
    if password2 != password:
        print('Suas senhas não coincidem, tente novamente.')
    else:
        break
print('Usuário registrado com sucesso.')

Behold working in the ideone. And in the repl it.. Also put on the Github for future reference.

  • 2

    If there are any mistakes, please inform me so I can correct.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.