Abdul Gandal
Abdul Gandal

Reputation: 97

Python prints 3 times

Why does python prints andata/ritorno 3 times? I've tried to outdent if, but it doesnt work

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")

Output:

andata
andata
andata

Upvotes: 0

Views: 99

Answers (1)

Błotosmętek
Błotosmętek

Reputation: 12927

You have two nested loops using the same variable:

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!

Whatever you tried to do, this seems wrong.

Upvotes: 2

Related Questions