简体   繁体   English

Python - 检查数字,大写,小写字母和特殊字符的输入

[英]Python - Checking an input for numbers, uppercase, lowercase letters and special characters

I am trying to create a little program in which the user enters a password into the console. 我正在尝试创建一个小程序,用户在其中输入密码到控制台。 The program then checks whether the password is weak, medium or strong depending what the user has typed in. 然后程序根据用户输入的内容检查密码是弱,中等还是强。

I need to check how many uppercase, lowercase and numbers have been entered and then tell the user how strong their password is. 我需要检查输入了多少大写,小写和数字,然后告诉用户他们的密码有多强。

I have most of the program completed, but since I haven't really used Python for long I am not too familiar with anything advanced, bear in mind younger people have to understand this code too who are not going to be good at coding themselves. 我已经完成了大部分的程序,但由于我没有真正使用Python很长时间,我对先进的东西不太熟悉,请记住,年轻人也必须理解这些代码,他们不会擅长编码。

So far I have: 到目前为止,我有:

#Welcome the user to the application.
print("Hello, please enter a password to check how secure it is");

#Set a variable called MinPass and set a value of 6.
MinPass = 6;

#Set a variable called MaxPass and set a value of 12.
MaxPass = 12;

#Set variable EnteredPass and wait for user input
EnteredPass = input("Password: ");

while len(EnteredPass) < MinPass:
    print("Your password is too short, please enter a longer password and try again")
    EnteredPass = input("Password: ");

while len(EnteredPass) > MaxPass:
    print("Your password is too long, please shorten it and try again!");
    EnteredPass = input("Password: ");

Please note, this is for educational purposes only. 请注意,这仅用于教育目的。 I'm not planning on making a program in an intent to steal random password. 我不打算制作一个旨在窃取随机密码的程序。 It's for a part of a course that's going on in my school! 这是我学校正在进行的课程的一部分!

This contains a few more-advanced concepts, but should be easy enough to follow: 这包含一些更高级的概念,但应该很容易遵循:

import string

def long_enough(pw):
    'Password must be at least 6 characters'
    return len(pw) >= 6

def short_enough(pw):
    'Password cannot be more than 12 characters'
    return len(pw) <= 12

def has_lowercase(pw):
    'Password must contain a lowercase letter'
    return len(set(string.ascii_lowercase).intersection(pw)) > 0

def has_uppercase(pw):
    'Password must contain an uppercase letter'
    return len(set(string.ascii_uppercase).intersection(pw)) > 0

def has_numeric(pw):
    'Password must contain a digit'
    return len(set(string.digits).intersection(pw)) > 0

def has_special(pw):
    'Password must contain a special character'
    return len(set(string.punctuation).intersection(pw)) > 0

def test_password(pw, tests=[long_enough, short_enough, has_lowercase, has_uppercase, has_numeric, has_special]):
    for test in tests:
        if not test(pw):
            print(test.__doc__)
            return False
    return True

def main():
    pw = input('Please enter a test password:')
    if test_password(pw):
        print('That is a good password!')

if __name__=="__main__":
    main()

You can use the following functions:- 您可以使用以下功能: -

    isalnum() # To check for alpha numeric characters
    isalpha() # To check for the presence of only alphabets

for eg. 例如。

    password = input("Enter the password: ")
    if password.isalnum() == False:
        print "Password should contain atleast one special character or number"
    if password.isalpha() == False:
        print "Password should contains atleast some alphabets."

To check for the presence of capital or small letters you can use this:- 要检查是否存在大写或小写字母,您可以使用: -

    temp = passsword.lower()
    if temp  == password:
        print "Password should contain atleast one capital letters"

How it Works? 这个怎么运作? Although this code is self-explanatory, there is nothing rocket-science about it, I will explain it since, you seem to be a beginner to me:- 虽然这段代码是不言自明的,但没有任何关于它的火箭科学,我将解释它,因为你似乎是我的初学者: -

    str.isalnum() returns False if the string contains some non alphabet characters
    str.isalpha() returns false if the string does not contain a single alphabet

By creating 通过创造

  temp

I am storing a copy of the password which is lowercase. 我存储的是小写密码的副本。 So if the password variable contains some capital letters then the 因此,如果密码变量包含一些大写字母,那么

    temp == password

will return False, and you can find out whether the string contains some capital letters or not. 将返回False,您可以找出该字符串是否包含一些大写字母。 As pointed out by @MartijnPieters, you can use 正如@MartijnPieters所指出的那样,你可以使用

    if not password.isalnum():

also instead of 而不是

    if password.isalnum() == False: 

Although, the first way is more "pythonic" 虽然,第一种方式更“pythonic”

编写MATLAB代码以检查输入密码的强度,代码应输出:'weak','medium','strong'和very strong',具体取决于密码的长度,使用大写和小写字母,使用数字和特殊字符。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 我们如何结合两个活动 - 比如说一个,删除数字,特殊字符和两个,将大写字母转换为小写字母,然后返回 - How do we combine two activities - say one, remove numbers, special characters and two, convert uppercase to lowercase letters, and back 不将 python 中的字母转换为大写和小写 - Not converting letters to uppercase and lowercase in python 以大写字符和数字拆分字符串,忽略 Python 中的连续大写字母 - Split a string at uppercase characters and numbers ignoring consecutive uppercase letters in Python 排序 python(输出大写/小写字母) - sort python ( print Uppercase/Lowercase letters in out) 在python中将特定字母转换为大写或小写 - Converting specific letters to uppercase or lowercase in python 用自己的 Function 计数数字、大写字母、小写字母和空格 - Counting Numbers, Uppercase Letters, Lowercase Letters and Spaces with own Function 用小写字母、大写字母和数字对字典的键进行排序 - sorting keys of a dictionary with lowercase letters, uppercase letters and numbers 用于验证特殊字符和小写字母的 if 语句 - If statements to validate special characters and lowercase letters 如何在python中将UTF大写字符转换为小写 - How to convert UTF uppercase characters to lowercase in python 带特殊字符的大写函数Python - Uppercase Function with Special Characters Python
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM