为什么 python 打印了 3 次 andataritorno?我试着去掉if,但没有用。
listlf = []
connf = sqlite3.connect("Database.db")
coursorf = connf.cursor()
sqltf = coursorf.execute("select np,id from orari")
for lf1 in sqltf:
lf2 = [lf1[0],lf1[1]]
listlf.append(lf2)
for lf3 in listlf:
if town == str(lf3[0]) and bs == str(lf3[1]):
loc1 = listlf.index(lf3)
for lf3 in listlf:
if des_town == str(lf3[0]) and des_bs == str(lf3[1]):
loc2 = listlf.index(lf3)
if loc1 < loc2:
print("andata")
else:
print("ritorno")
输出。
andata
andata
andata
你有两个嵌套循环使用同一个变量。
for lf3 in listlf: # lf3 !
if town == str(lf3[0]) and bs == str(lf3[1]):
loc1 = listlf.index(lf3)
for lf3 in listlf: # lf3 too!
不管你想做什么,这似乎是错误的。