for c in password:
if not any(c in password for c in symbols):
return False
if not any(c.isdigit() for c in password):
return False
if not any(c.islower() for c in password):
return False
if not any(c.isupper() for c in password):
return False
return
print(password)
f = open("demofile2.txt", "a")
f.write(password "\n")
f.close()
CodePudding user response:
You want to avoid doing extra work when verifying that your password satisfies all requirements. In my example, I've looked at each character only once and am performing the required checks every time I look at a different character.
In the example you provided, you're actually verifying that every character meets your requirements for every character in the password O(N*N) - this is not recommended.
Lastly, make sure you understand what the password requirements are. It seems like your logic is incorrect when trying to verify that a character is both lowercase and uppercase simultaneously.
satisfies_requirements = True
for c in password:
# if (c not in symbols) and (not c.isalnum()): <- something like this perhaps
if (c does not satisfy some requirement):
satisfies_requirements = False
break
if satisfies_requirements:
print(password)
f = open("demofile2.txt", "a")
f.write(password "\n")
f.close()
else:
return False
CodePudding user response:
you should take care about time complexity by avoiding unnecessary loops.
this will help you:
from string import punctuation
def check_pass(password):
return all([any(filter(str.islower, password)),
any(filter(str.isupper, password)),
any(filter(str.isdigit, password)),
any(filter(lambda x: x in punctuation, password))
]
)
def save_password(password):
if check_pass(password):
with open("demofile2.txt", "a") as f:
f.write(password)
print(password)
else:
print('Not Accepted!')
# Test
save_password('1@cA')