Home > Net >  unclear behavior in for loop with python
unclear behavior in for loop with python

Time:10-03

i have this code in python:

query = "SELECT product_id FROM product_orders WHERE table_number = " e 
    cursor.execute(query)
    records = cursor.fetchall()
    for record in records:
        query2 = "SELECT * FROM productss WHERE id = " str(record[0])
        cursor.execute(query2)
        record2 = cursor.fetchall()
        sum=0
        for record1 in record2:
            sum = sum   record1[2]
            tree.insert("", tk.END, values=record1)
        
    tree2.insert("", tk.END, values=sum)

the problem is the sum variable does not make summing, but store only the last value of record1 . any solution for this ?

CodePudding user response:

It looks like you have sum = 0 inside of a for loop. Maybe if you take it out of the loop and make it a list of sums that will fix your issue. Also, as another user has said, sum is a built-in name, you can name your variable s instead.

  • Related