Python Day Day Up 003
密码输入格式检查
- 要求密码长度为 6 到 20 位
- 密码只包含英文字母和数字
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

浙公网安备 33010602011771号