Python Day Day Up 003

密码输入格式检查

  1. 要求密码长度为 6 到 20 位
  2. 密码只包含英文字母和数字
import re

def checkPassword():
    pw = input("please input your password:")
    pwObj = re.compile(r'[\da-zA-Z]{6,20}')
    
    if pwObj.fullmatch(pw) == None:  #用fullmatch()方法查看是否整个字符串都匹配
        print("\nCheck Out:Incorrect password format.\n")
        #print(pwObj.fullmatch(pw))
        return checkPassword()
    else:
        print("\nCheck Out:Correct format.\n")
        #print(pwObj.fullmatch(pw))
        print("Your password is :{}".format(pw))

checkPassword()

 

运行:(格式正确下)

please input your password:python123

Check Out:Correct format.

Your password is :python123

 

运行:(格式不正确下)

please input your password:123

Check Out:Incorrect password format.

please input your password:123456

Check Out:Correct format.

Your password is :123456

 

posted @ 2020-04-24 17:55  生命就是不停地雕刻时光  阅读(176)  评论(0)    收藏  举报