嗨,所以我对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:语法无效
提前致谢!
答案 0 :(得分:0)
为了使用try & except
函数,您需要第一部分try
。在您的代码中没有try:
,这就是except
导致问题的原因。
然而,每次我尝试修复一个问题时,都会弹出另一个问题。但是,如果语法不正确,Python甚至不会运行您的代码。
为了修复语法错误,
更改
else:
print ("none")
except IndexError:
pass
要
用try
语句包裹整件事,
然后except
会抓住IndexError