1

I'm currently starting to learn Python and chose Al Sweigart's "Automate the Boring Stuff with Python" to help me with my first steps. As I really like the look and feel of Visual Studio Code I tried to switch after the first part of the book.

The following code is from the online material and should therefore be correct. Unfortunately it works fine in IDLE but not in VS Code.

def isPhoneNumber(text):
    if len(text) != 12:
        return False  # not phone number-sized
    for i in range(0, 3):
        if not text[i].isdecimal():
            return False  # not an area code
    if text[3] != '-':
        return False  # does not have first hyphen
    for i in range(4, 7):
        if not text[i].isdecimal():
            return False  # does not have first 3 digits
    if text[7] != '-':
        return False  # does not have second hyphen
    for i in range(8, 12):
        if not text[i].isdecimal():
            return False  # does not have last 4 digits
    return True  # "text" is a phone number!

print('415-555-4242 is a phone number:')
print(isPhoneNumber('415-555-4242'))
print('Moshi moshi is a phone number:')
print(isPhoneNumber('Moshi moshi'))

I get the following error:

    415-555-4242 is a phone number: 
    Traceback (most recent call last):   
File "/Users/.../isPhoneNumber.py", line 20, in <module>
            print(isPhoneNumber('415-555-4242'))   
File "/Users/.../isPhoneNumber.py", line 5, in isPhoneNumber
            if not text[i].isdecimal(): AttributeError: 'str' object has no attribute 'isdecimal'

I'd be happy about your suggestions to make it work. I already installed the Python Extension and installed suggested stuff with pip3.

Thanks in advance.

2
  • What python interpreter are you using? 3.x strings have isdecimal, but 2.x does not. Commented Oct 16, 2016 at 21:31
  • I wasn't really sure which interpreter I'm specifically using. But due to your response I looked it up and changed it to the 3.x interpreter. Now it works! Thanks! Commented Oct 16, 2016 at 22:37

1 Answer 1

1

Only Unicode strings have isdecimal(), so you'd have to mark it as such.

To convert a string to a unicode string in python, you can do this:

s = "Hello!"
u = unicode(s, "utf-8")  

In your question you can just change print(isPhoneNumber('415-555-4242')) to print(isPhoneNumber(u'415-555-4242')) and print(isPhoneNumber('Moshi moshi')) to print(isPhoneNumber(u'Moshi moshi'))

u'string' in python determines that the string is a unicode string

Sign up to request clarification or add additional context in comments.

3 Comments

wow thanks. Haven't known this before. Changed the interpreter, now it works fine.
No problem. Mark it as correct answer so that it would be helpful for others.
I didn't realize that all unicode strings have isdecimal - thanks!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.