-2

How would I validate an input to check if it contains at least 1 number, ONE CAPITAL letter and one lower case letter. I have looked through similar questions and they don't seem to be exactly what I need e.g only checking for numbers and rejecting letters, I need it to except anything with more than one number, capital letter and lower case letter.

user8435959
  • 165
  • 1
  • 11

2 Answers2

0

You can use assert combined with any() and string built-in module like this:

import string

password = '1Aa'

try:
    assert any(i in string.ascii_lowercase for i in password)
    assert any(i in string.ascii_uppercase for i in password)
    assert any(i in string.digits for i in password)
except AssertionError as e:
    raise Exception('Invalid password!')
ettanany
  • 19,038
  • 9
  • 47
  • 63
0
import re
def passwordagain():
password()

def password():
        print()
        password = input("Enter a secure password: ")
        if   re.search('[A-Z]',password) is None:
            print("Password must contain at least one capital letter and one number.")
            passwordagain() 
        elif re.search('[0-9]',password) is None:
            print("Password must contain at least one capital letter and one number.")
            passwordagain() 
        elif re.search('[a-z]',password) is None: 
            print("Password must contain at least one capital letter and one number.")
            passwordagain()          
        else:
            filename = ("password.");
            with open (filename, "w") as f:
                f.write (password)
            print()
            print("Password saved")
user8435959
  • 165
  • 1
  • 11