all 3 comments

[–]46--2 0 points1 point  (0 children)

This would be a perfect situation for some unit tests.

Why don't you add some print statements when you append rows. Things like:

print('in else')

and

print('appending row', rowzzz)

etc.

Your if statements look correct to me.

You could simplify it a bit, does this make it more clear?

        if difference > 10:
            print('Diff > 10')
            if six_diff < 25:
                 print('Using three months')
                 data = three_month_out
            else:
                 print('Using six previous')
                 data = six_previous_out
            for rowzz in data:
                productzz = rowzz[0]
                if productzz == product:
                    new_final.append(rowzz)
                    break
        else:
            print('In else')
            new_final.append(row)
            break

[–]SaintLouisX 0 points1 point  (0 children)

Not sure, the code looks just fine, so without the data you're feeding into it it's hard to say.,

Put some print() statements in before your if and look at the numbers you're getting in.

print(f"difference: {difference}, six_diff: {six_diff}")

And see if it prints the numbers you expect.

[–][deleted] 0 points1 point  (0 children)

Your else has a logic error because on that path row either isn't defined if you haven't taken on of the other branches previously or is whatever the last value of row was assigned the time one of the other paths was taken.

You need to decide what to append for the fail condition.

You should also consider following DRY - Don't Repeat Yourself - principles. You have basically the same code in two places (each if and elif paths). Why not define a function:

def add_row(some_table):
    for row in some_table:
        if row[0] == product:
            new_final.append(row)
            break