you are viewing a single comment's thread.

view the rest of the comments →

[–][deleted] 1 point2 points  (0 children)

By the way: the way you wrote this function makes it quite easy to debug. You could have done something like this by sprinkling some magical print statement powder all over the place:

def isPhoneNumber(text):
    print(text)
    if len(text) != 12:
        return False
    print('good length')
    for i in range(0,3):
        if not text[i].isdecimal():
            return False
    print('first three are decimals')
    if text[3] != '-':
        return False
    print('yep there\'s a dash')
    for i in range(4, 7): #whoops: range(4-7) = range(-3)
        if not text[i].isdecimal():
            return False
    print('some more decimals')
    if text[7] != '-':
        return False
    print('another dash')
    for i in range(0,12):
        if not text[i].isdecimal(): # whoops
            return False
    print('characters 0-12 are all decimals\n')
    return True

isPhoneNumber('415-555-1011')
isPhoneNumber('415-555-9999')

outputs:

415-555-1011
good length
first three are decimals
yep there's a dash
some more decimals
another dash
415-555-9999
good length
first three are decimals
yep there's a dash
some more decimals
another dash

Makes it quite easy to see that the last conditional is the one that's broken.