-2

So here I am again improving on my online login thingy.

Anyways I've got it all mostly working but I just cant seem to get the password authentication fully working. It will tell people the password is invalid if it is less than 7 characters but I want it to do the same if there isn't a capital letter in the password.

I spent the last 20 minutes looking through the web and trying lots of different things and none seem to have worked, the minimum numbers thing has though. Anyways heres my code atm:

password = input("Enter a password: ")
capital = password.upper().isupper()
while len(password) < 7 and capital is False:
    print("Your password must be at least 7 characters long including A capital letter")
    password = input("Enter a password: ")
Nitroxc
  • 15
  • 1
  • 4

1 Answers1

3

To check if the password contains at least one uppercase letter, you can use:

has_uppercase = any(c.isupper() for c in password)

See the documentation for the any function.

For instance:

>>> any(c.isupper() for c in "secr3t")
False
>>> any(c.isupper() for c in "Secr3t")
True

Since, Python doesn't have do ... while ... loop, you can use an infinite loop like this:

while True:
    password = input("Enter a password: ")
    if len(password) > 7 and any(c.isupper() for c in password):
        break
    print("Your password must be at least 7 characters long including A capital letter")
print("What a secured password!")

You can try some password:

Enter a password:  secret23
Your password must be at least 7 characters long including A capital letter

Enter a password:  Secr3t
Your password must be at least 7 characters long including A capital letter

Enter a password:  Secr3t123
What a secured password!
Community
  • 1
  • 1
Laurent LAPORTE
  • 21,958
  • 6
  • 58
  • 103
  • Needs same check for lower case I assume as well to make sure that ALL chars are not in upper case (assume that would be requirement as well for this case ) – danchik Dec 08 '16 at 20:43
  • 1
    @danchik: You can combine the two conditions: `any(c.isupper() for c in password) and any(c.islower() for c in password)`. – Laurent LAPORTE Dec 08 '16 at 20:44
  • Whilst this did work in validating there is a capital in there, i would enter the password, then be greated with another box asking for password and only then after entering a wrong password there would i tell me about the requirements. anyways i got it working now with patricks fix – Nitroxc Dec 08 '16 at 20:45
  • To fix that, just put the thing that prints the requirements above the `if` statement. – Iluvatar Dec 08 '16 at 20:49