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表达式。

天野
1 声望0 粉丝