Click here to Skip to main content
15,887,434 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
The output of my code is as follows:

1 4 6 8
1 4 6 9
1 4 7 9
1 5 7 9


The desired output is as follows:

1 4 6 8 
1 4 6 9 
1 4 7 8 
1 4 7 9 
1 5 6 8
1 5 6 9 
1 5 7 8 
1 5 7 9


I want the loop to go through all possible combinations. Any help here is much appreciated.

What I have tried:

var_a = ["1"]
var_b = ["4", "5"]
var_c = ["6", "7"]
var_d = ["8", "9"]

for x in var_a:
    for y in var_b:
        for z in var_c:
            for a in var_d:
                var_a=x 
                var_b=y
                var_c=z
                var_d=a
                print(var_a,var_b,var_c,var_d)
Posted
Updated 6-May-22 10:59am
v2

Try this
Python
var_a = ["1"]
var_b = ["4", "5"] # this variable
var_c = ["6", "7"]
var_d = ["8", "9"]

for x in var_a:
    for y in var_b:
        for z in var_c:
            for a in var_d:
                var_a=x 
                var_b=y # is destroyed  here
                var_c=z
                var_d=a
                print(var_a,var_b,var_c,var_d)
                print(x,y,z,a)
 
Share this answer
 
v2
You should use numeric loops rather than characters. You can then set range values for each one. When a variable reaches its limit you then start the next value to the left. The innermost loop will go from 8 to 9, the next from 6 to 7, etc.
 
Share this answer
 
Why don't you break this into 3 functioning loops and then add them up. Honestly I didn't check the code but personally I preffer the method I just told.
It might take some time but the answer worth the time.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900