How to check if you have a number in your Python password?

Asked

Viewed 49 times

0

I need help on a question about Python strings. The statement says the following:

Write a program to read a password and check if it contains the security requirements: At least one uppercase character and one lowercase character, as well as containing numbers and letters, as well as at least 6 and at most 10 characters.

In the code I made, everything was right, until the moment I had to check if there were numbers in the password. The code is this:

while True:
  senha = input('Digite sua senha: ')
  list(senha)
  if len(senha) < 6:
    print('Deve conter no mínimo 6 caracteres.')
  elif len(senha) > 10:
    print('Deve conter no máximo 10 caracteres.')
  else:
    if any(x.isupper() for x in senha):
      print('Deve conter pelo menos uma letra minúscula.')
    elif any(x.islower() for x in senha):
      print('Deve conter pelo menos uma letra maiúscula.')
    elif any(x.isdigit() == False for x in senha):
      print('Deve conter pelo menos um número.')
    else:
      break
    
print('Senha válida.')

No output tried several possible situations, to test all security requirements, but then gave problem in the last requirement that is in the code. The result in the output gave this:

Digite sua senha: ash
Deve conter no mínimo 6 caracteres.
Digite sua senha: Charlie12345
Deve conter no máximo 10 caracteres.
Digite sua senha: r4gn4rok
Deve conter pelo menos uma letra maiúscula.
Digite sua senha: H41LM4RY
Deve conter pelo menos uma letra minúscula.
Digite sua senha: Brazuka
Deve conter pelo menos uma letra minúscula.

NOTE: In the last line should have given a warning that the password must contain at least one number, but instead this message appears.

How do I solve this? Could you help me with this code?

  • 1

    I believe the ideal approach would be to use Regex, for each condition a regex and see if it passes the test

  • 1

    https://answall.com/q/443332/112052

1 answer

0

There are several ways you can perform these checks more elegantly, safely and performatively, but I’ll just focus on explaining why your code didn’t work to get if there are numbers in the password.

The main reason is that the way you are using the function any() is inverted. It is reversed for all conditions, and so is stopping its code in the first condition "check if it has at least one uppercase letter".

if any(x.isupper() for x in senha):
    print('Deve conter pelo menos uma letra minúscula.')

First, as the any() works:

  • if any item in the list is true, any returns True
  • if ALL list items are false, any returns False

So, basically, your elif can be read from the following as "if any item on my list is an uppercase letter, return the message that it should contain at least one lowercase letter". While what you want is something like "if NO item on my list is an uppercase letter, return the message that it should contain an uppercase letter".

The way to fix this in current code is by denying the condition and reversing the message.

if not any(x.isupper() for x in senha):
  print('Deve conter pelo menos uma letra maiúscula.')

This has to be done for all conditions using the any().

Browser other questions tagged

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