lundi 17 juin 2019

If Statement Inside Nested For Loop

The If statement inside my nested for loop is not executing. Instead, the code essentially ignores it and prints all possible combinations of x_1, x_2, x_3, x_4, x_5, x_6, and x_7 instead of the ones specified by the if statement.

I've tried avoiding using a nested for loop altogether using zip, but this only looks at combinations of x_1, x_2, x_3, x_4, x_5, x_6 and x_7 where x_1 == x_2 == x_3 == x_4 == x_5 == x_6 == x_7. I've also tried placing the if statement in various places, but each time, the code ignores the statement.

for a in range(-5,6):
    for b in range(-5,6):
        for c in range(-5,6):
            for d in range(-5,6):
                for e in range(-5,6):
                    for f in range(-5,6):
                        for g in range(-5,6): 
                            for x_1 in range(-5,6): 
                                for x_2 in range(-5,6): 
                                    for x_3 in range(-5,6): 
                                        for x_4 in range(-5,6): 
                                            for x_5 in range(-5,6): 
                                                for x_6 in range(-5,6): 
                                                    for x_7 in range(-5,6):
                                                        if a*x_1 + b*x_2 + c*x_3 + d*x_4 + e*x_5 + f*x_6 + g*x_7 == 7 and x_1^2 + x_2^2 + x_3^2 + x_4^2 + x_5^2 + x_6^2 + x_7^2 == 7:
                                                            print x_1, x_2, x_3, x_4, x_5, x_6, x_7                                                                                                                        
print "DONE"

It shouldn't output every single combination of x_1, x_2, x_3, x_4, x_5, x_6 and x_7 as it does not. It should only output, for example, 1, 1, 1, 1, 1, 1, 1 -- this is a combination of x_1, x_2, x_3, x_4, x_5, x_6, and x_7 such that the sum of all the squares of the seven variables is equal to 7 and ax_1 + bx_2 + cx_3 + dx_4 + ex_5 + fx_6 + g*x_7 is equal to 7 for some a,b,c,d,e,f, and g each any number in range(-5,6) (in particular, in this example, each of these 7 letter variables is just equal to 1). All 7 of x_1, x_2, ..., x_7 need not be equal, either.

Aucun commentaire:

Enregistrer un commentaire