Day 1:Regex validate PIN code

Details:
ATM machines allow 4 or 6 digit PIN codes and PIN codes cannot contain anything but exactly 4 digits or exactly 6 digits.
If the function is passed a valid PIN string, return true, else return false.
eg:
validate_pin("1234") == True
validate_pin("12345") == False
validate_pin("a234") == False
My Solution:
def validate_pin(pin):
    m = re.match(r'(\d{4}$)|(\d{6}$)', pin)
    if m:
        return True
    else:
        return False
Best Practice:
def validate_pin(pin):
    return len(pin) in (4, 6) and pin.isdigit()
Tips:
1. 判断字符是否为数字可以用isdigit()方法。
2. 判断某个值是否属于某个集合可以用in表达式。
    原文作者:天野
    原文地址: https://segmentfault.com/a/1190000014621629
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞