A password must have at least eight characters. • A password consists of only letters and digits. • A password must contain at least two digits
2 Answers
you can refer to this code as your answer :
import re
def validate():
while True:
password = input("Enter a password: ")
if len(password) < 8:
print("Make sure your password is at lest 8 letters")
elif re.search('[0-9]',password) is None:
print("Make sure your password has a number in it")
elif re.search('[A-Z]',password) is None:
print("Make sure your password has a capital letter in it")
else:
print("Your password seems fine")
break
validate()
Comments
if you want to generate such passwords use this code:
import string
import random
letters =string.ascii_letters
digits = string.digits
comb = letters + digits
length = random.randint(6,98)
password = random.choices(digits,k = 2)
password+= random.choices(comb, k =length)
random.shuffle(password)
password = ''.join(password)
print(password)
I assumed the max length of a password is 100. you may want to change it.