Python Code works in IDLE but not in VS Code
<p>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. </p>

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

<pre><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'))
</code></pre>

<p>I get the following error:</p>

<pre><code>    415-555-4242 is a phone number: 
    Traceback (most recent call last):   
File "/Users/.../isPhoneNumber.py", line 20, in &lt;module&gt;
            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'
</code></pre>

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

<p>Thanks in advance.</p>

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

<p>To convert a string to a unicode string in python, you can do this:</p>

<pre><code>s = "Hello!"
u = unicode(s, "utf-8")  
</code></pre>

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

<blockquote>
  <p>u'string' in python determines that the string is a unicode string</p>
</blockquote>

python vscode