Validation of a Password - Python

前端 未结 11 1992
自闭症患者
自闭症患者 2020-11-27 08:20

So I have to create code that validate whether a password:

  • Is at least 8 characters long
  • Contains at least 1 number
  • Contains at least 1
11条回答
  •  半阙折子戏
    2020-11-27 08:50

    uppercase_letter = ['A', 'B','C', 'D','E','F','G','H','I','J','K','L','M','N','O',
                'P','Q','R','S','T','U','V','W','X','Y','Z']
    
    number = [1, 2, 3, 4, 5, 6, 7, 8, 9, 0]
    
    import re
    
    info ={}
    
    while True:
    
        user_name = input('write your username: ')
    
        if len(user_name) > 15:
            print('username is too long(must be less than 16 character)')
        elif len(user_name) < 3 :
            print('username is short(must be more than 2 character)')
        else:
            print('your username is', user_name)
            break
    
    
    while True:   
    
        password= input('write your password: ')
    
        if len(password) < 8 :
            print('password is short(must be more than 7 character)')
    
        elif len(password) > 20:
            print('password is too long(must be less than 21 character)') 
    
        elif re.search(str(uppercase_letter), password ) is None :
            print('Make sure your password has at least one uppercase letter in it')
    
        elif re.search(str(number), password) is None :
            print('Make sure your password has at least number in it')
    
        else:
            print('your password is', password)
            break
    
    info['user name'] = user_name
    
    info['password'] = password
    
    print(info)
    

提交回复
热议问题