如何在python中创建密码检查器?

9vw9lbht  于 2021-08-20  发布在  Java
关注(0)|答案(2)|浏览(478)

关闭。这个问题需要更加关注。它目前不接受答案。
**想改进这个问题吗?**编辑这篇文章,更新这个问题,使它只关注一个问题。

两天前关门了。
改进这个问题
我正在尝试用python制作一个简单的密码检查器。该程序要求用户输入一个包含8个以上字母/符号和if/else语句的密码,如果它不包含上/下字母和数字,但每次我输入一些东西时,它都会打印“密码足够强”,即使我没有输入上/下字母或数字。所以,如果有人能帮助我,我会非常感激。
这是代码:

password = input("Input your password: ")

if (len(password)<8):
  print("Password isn't strong enough")
elif not ("[a-z]"):
  print("Password isn't strong enough")
elif not ("[A-Z]"):
  print("Passsword isn't strong enough")
elif not ("[0-9]"):
  print("Password isn't strong enough")
else:
  print("Password is strong enough")
7d7tgy0s

7d7tgy0s1#

此支票:

elif not ("[a-z]"):

什么都不做;它只是检查静态字符串的真值。自从 "[a-z]" 是一个非空字符串,它总是被认为是true(或“truthy”),这意味着 not "[a-z]" 无论发生什么,都是假的 password . 您可能想使用 re 模块,您可以在此处阅读:https://docs.python.org/3/library/re.html
下面是一种不用正则表达式,使用python的 allany 功能,它的 in 关键字,以及 string 包含方便字符串的模块,如 ascii_lowercase (所有小写字母,对应于正则表达式字符类 [a-z] ):

import string

password = input("Input your password: ")

if all([
    len(password) >= 8,
    any(c in password for c in string.ascii_lowercase),
    any(c in password for c in string.ascii_uppercase),
    any(c in password for c in string.digits),
]):
    print("Password is strong enough")
else:
    print("Password is not strong enough")
o7jaxewo

o7jaxewo2#

您可以使用正则表达式简单地执行此操作,它将很好地工作:

import re
password = input("Input your password: ")

if (re.match(r"^.*[A-Z]", password) and re.match(r"^.*[0-9]", password) and len(password)>7 and re.match(r"^.*[a-z]", password) ):   
    print("Password is strong enough")
else:
    print("Password is not strong enough")

相关问题