Getting a SyntaxError in try:except (python)
<p>Hi so I'm really new to Python and I have a little question.</p>

<p>In my code:</p>

<pre><code>from collections import Counter
</code></pre>

<p>try:
    while True:</p>

<pre><code>    name1 = input ("your name")
list(name1)
name1len = len(name1)

name2 = input ("other one's name")
list(name2)
name2len = len(name2)

if name1len &gt; 10:
    print ("name is too long")
    break 
if name2len &gt; 10:
    print ("name is too long")
    break
a1 = (name1[0][0])
a2 = (name2[0][0])
if set(a1) &amp; set(a1) == set(a2):
    print ("ok")
else:
    print ("none")

a3 = (name1[1][0])
a4 = (name2[1][0])
if set(a3) &amp; set(a3) == set(a4):
    print ("ok")
else:
    print ("none")

a5 = (name1[2][0])
a6 = (name2[2][0])
if set(a5) &amp; set(a5) == set(a6):
    print ("ok")
else:
    print ("none")
a7 = (name1[3][0])
a8 = (name2[3][0])
if set(a7) &amp; set(a7) == set(a8):
    print ("ok")
else:
    print ("none")

a9 = (name1[4][0])
a10 = (name2[4][0])
if set(a9) &amp; set(a9) == set(a10):
    print ("ok")
else:
    print ("none")
a11 = (name1[5][0])
a12 = (name2[5][0])
if set(a11) &amp; set(a11) == set(a12):
    print ("ok")
else:
    print ("none")
a13 = (name1[6][0])
a14 = (name2[6][0])
if set(a13) &amp; set(a13) == set(a14):
    print ("ok")
else:
    print ("none")
a15 = (name1[7][0])
a16 = (name2[7][0])
if set(a15) &amp; set(a15) == set(a16):
    print ("ok")
else:
    print ("none")
a17 = (name1[8][0])
a18 = (name2[8][0])
if set(a17) &amp; set(a18) == set(a19):
    print ("ok")
else:
    print ("none")
a19 = (name1[9][0])
a20 = (name2[9][0])
if set(a19) &amp; set(a19) == set(a20):
    print ("ok")
else:
    print ("none")
a21 = (name1[10][0])
a22 = (name2[10][0])
if set(a21) &amp; set(a21) == set(a22):
    print ("ok")
else:
    print ("none")

    except (IndexError):
    pass
</code></pre>

<p>and in the end at the very bottom, it keeps on giving me this error:</p>

<p>File "dr.luvtest.py", line 106
except (IndexError):
     ^
SyntaxError: invalid syntax</p>

<p>Thanks in advance! </p>

<p>In order to use the <code>try &amp; except</code> functions, you need to you that first part, the <code>try</code>. In your code there is no <code>try:</code> which is why the <code>except</code> is causing issues.</p>

<p>However every time I try to fix one issue, other one pops up. However Python won't even run your code if the syntax is incorrect.</p>

<p>In order to fix the syntax error,</p>

<p>Change</p>

<pre><code>else:
    print ("none")

except IndexError:
    pass
</code></pre>

<p>To</p>

<p>Wrap the whole thing in a <code>try</code> statement,
then the <code>except</code> will catch an <code>IndexError</code></p>

python python-3.x