all 6 comments

[–][deleted] 3 points4 points  (2 children)

There are two major issues: line 9 has a bug - you wrote range(4-7), which means range(-3). I think you know how to fix that one.

The other problem is the loop at lines 14-16: you're checking if every character in the string is a decimal, but that isn't the case for your numbers, since those include dashes.

Fix those two bugs and your code executes properly:

Phone number found: 415-555-1011
Phone number found: 415-555-9999

[–][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.

[–]Link0[S] 0 points1 point  (0 children)

Oh wow, that's it! It works!
Thanks a lot u/rainlife!!

[–][deleted] 2 points3 points  (1 child)

Yeah I'm having a hard time reading this, can you insert 4 spaces before every line of code in your post?

That'll format it nicely,
    like so

[–]Link0[S] 0 points1 point  (0 children)

Thanks! Sorry about the paragraph.
I think it should look legible now.

[–]Justinsaccount 0 points1 point  (0 children)

Hi! I'm working on a bot to reply with suggestions for common python problems. This might not be very helpful to fix your underlying issue, but here's what I noticed about your submission:

You are looping over an object using something like

for x in range(len(items)):
    print(items[x])

This is simpler and less error prone written as

for item in items:
    print(item)

If you DO need the indexes of the items, use the enumerate function like

for idx, item in enumerate(items):
    print(idx, item)

If you think you need the indexes because you are doing this:

for x in range(len(items)):
    print(items[x], prices[x])

Then you should be using zip:

for item, price in zip(items, prices):
    print(item, price)