我是靠谱客的博主 聪明小伙,这篇文章主要介绍python检查密码字符串是否规范_密码检查Python 3,现在分享给大家,希望可以做个参考。

根据isdigit()方法的以下引用here:This method returns true if all characters in the string are digits

and there is at least one character, false otherwise.

这不适合你的案子A password must contain at least 2 digits

该方法只让您知道给定的字符串是否是数字,而不是字符串中有多少数字。要做到这一点,你需要多加练习。

您可以使用以下命令if sum(character.isdigit() for character in password) >= 2:

此外,您的代码有一个小错误,因为您永远不会返回True。下面是一个可能的解决方案:def CountDigitsFor(password):

return sum(character.isdigit() for character in password)

def validPassword(password):

if len(password) >= 8 and password.isalnum() and CountDigitsFor(password) >= 2:

return True

return False

另外,在你的主体中,当你从用户那里得到密码时,你有一个小的输入错误password = validPassword()

应该是password = getPassword()

因此这里有一个完整的代码def getPassword():

return input("Enter password: ")

def CountDigitsFor(password):

return sum(

最后

以上就是聪明小伙最近收集整理的关于python检查密码字符串是否规范_密码检查Python 3的全部内容,更多相关python检查密码字符串是否规范_密码检查Python内容请搜索靠谱客的其他文章。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(57)

评论列表共有 0 条评论

立即
投稿
返回
顶部