嗨,所以我對Python真的很陌生,我有一個小問題。在try中獲取SyntaxError:except(python)
在我的代碼:
from collections import Counter
嘗試: 而真:
name1 = input ("your name")
list(name1)
name1len = len(name1)
name2 = input ("other one's name")
list(name2)
name2len = len(name2)
if name1len > 10:
print ("name is too long")
break
if name2len > 10:
print ("name is too long")
break
a1 = (name1[0][0])
a2 = (name2[0][0])
if set(a1) & set(a1) == set(a2):
print ("ok")
else:
print ("none")
a3 = (name1[1][0])
a4 = (name2[1][0])
if set(a3) & set(a3) == set(a4):
print ("ok")
else:
print ("none")
a5 = (name1[2][0])
a6 = (name2[2][0])
if set(a5) & set(a5) == set(a6):
print ("ok")
else:
print ("none")
a7 = (name1[3][0])
a8 = (name2[3][0])
if set(a7) & set(a7) == set(a8):
print ("ok")
else:
print ("none")
a9 = (name1[4][0])
a10 = (name2[4][0])
if set(a9) & set(a9) == set(a10):
print ("ok")
else:
print ("none")
a11 = (name1[5][0])
a12 = (name2[5][0])
if set(a11) & set(a11) == set(a12):
print ("ok")
else:
print ("none")
a13 = (name1[6][0])
a14 = (name2[6][0])
if set(a13) & set(a13) == set(a14):
print ("ok")
else:
print ("none")
a15 = (name1[7][0])
a16 = (name2[7][0])
if set(a15) & set(a15) == set(a16):
print ("ok")
else:
print ("none")
a17 = (name1[8][0])
a18 = (name2[8][0])
if set(a17) & set(a18) == set(a19):
print ("ok")
else:
print ("none")
a19 = (name1[9][0])
a20 = (name2[9][0])
if set(a19) & set(a19) == set(a20):
print ("ok")
else:
print ("none")
a21 = (name1[10][0])
a22 = (name2[10][0])
if set(a21) & set(a21) == set(a22):
print ("ok")
else:
print ("none")
except (IndexError):
pass
,並在最底部的結束,它不斷給我這個錯誤:
文件「dr.luvtest.py」,第106行 (IndexError): ^ SyntaxError:無效的語法
在此先感謝!
請[編輯]確保您的縮進是正確的。 –
'except'必須與'try'處於同一縮進級別。 –
此外,該代碼甚至應該做什麼?比較兩個字符串之間的區別? 'name1 [0] [0]'與'name1 [0]'相同,因爲它是字符串的第一個字符 –