需要创建一个函数来验证我的密码强度是否遇到第三个问题。
3。如果密码的第一个和最后一个字母是字母,那么无论大小写如何,密码的第一个字符都必须与密码的最后一个字符不同。例如第一个字符为A,因此最后一个字符不能为A或a。
def validate_password(first_pwd, second_pwd):
#Checks if password are same
if first_pwd == second_pwd:
#Checks if password is greater or equal to 8 characters
if len(first_pwd) >= 8:
#Checks if last and first character are alphabetic
if first_pwd[0].islower() and first_pwd[-1].isupper() or first_pwd[0].isupper() and first_pwd[-1].islower():
return True
else:
return False
print(validate_password("Abcd1234","Abcd1234"))
如何忽略密码末尾的数字,并查看最接近的字母d。
答案 0 :(得分:0)
You might try to solve it with a regex. This checks if the first occurrence of a letter equals (case-insensitively) the last occurrence of a letter.
You have to adapt the regex or the if clause if you really just want to check the first char of the string or the last one.
You also can do an extra check to see if the last char in the pwd equals the last letter in the list chars below.
import re
def validate_password(first_pwd, second_pwd):
# Checks if password are same
if first_pwd == second_pwd:
# Checks if password is greater or equal to 8 characters
if len(first_pwd) >= 8:
# get only the letters of the password
chars = re.findall(r'[A-Za-z]', first_pwd)
# compare first and last occurrence of letters in pwd
if chars[0].islower() and chars[-1].isupper() or chars[0].isupper() and chars[-1].islower():
return True
else:
return False
print(validate_password("Abcd1234","Abcd1234"))
I also think, it might be enough only to check
if chars[0].upper() == chars[-1].upper(): # optional if needed: and first_pwd[-1].upper() == chars[-1].upper()
答案 1 :(得分:0)
I'm not an expert, but as someone who tries to continually improve at python it's recommended to write out the words instead of using abbreviations.
also you rother functions to verify the password strength are being checked only if the password was the same as the previous, so make sure you take length greater than 8 out of the first check
Things worth looking at would be:
islower, isalpha HERE
with lower() HERE
and I recommend youtubing regex because it's a tough concept imo
import re
def validate_password(first_password, second_password):
if first_password == second_password:
# Cannot be the previous password
return False
letters = "".join(re.findall("[a-zA-Z]+", first_password))
first_letter = letters[0]
last_letter = letters[-1]
if len(first_password) >= 8:
if first_letter.lower() == last_letter.lower():
# First character can't be the same as last character
return False
else:
# Every test passed
return True
else:
# password too short
return False
print(validate_password("Abcd1234","Abcd1234"))