I have a Python script that generates a dictionary of all possible 3 digit password combinations. It essentially works by just nesting for loops. This is fine, but say I wanted to change it so it could generate 4, 5, 6, ... digit combinations. This would require me to keep adding more nested loops and changing the print statement.How can I make my current code more efficient and apply the DRY principle? This way, if I wanted to change it to generate 4-digit codes, I could simply change a single variable rather then add an entire for loop.Current code:f=open('New Text Document.txt','w')def getCombos(digits, caps, numbers):    if(caps == True):        if(numbers == True):            for x in range(33, 126):               for y in range(33, 126):                   for z in range(33, 126):                       print(chr(x) +  + chr(y) +  + chr(z))                       f.write(chr(x) +  + chr(y) +  + chr(z) + \n)getCombos(3, True, True) If I wanted to add a fourth digit:f=open('New Text Document.txt','w')def getCombos(digits, caps, numbers):    if(caps == True):        if(numbers == True):            for x in range(33, 126):               for y in range(33, 126):                   for z in range(33, 126):                       for m in range(33, 126):                       print(chr(x) +  + chr(y) +  + chr(z) +  + char(m))getCombos(3, True, True)
Generate a dictionary of passwords
python;combinatorics
Please take a look at itertools.product. It will simplify your code greatly.def getCombos(digits, caps, numbers):    # Build set of choices appropriately based on caps and numbers    choices = list(range(33, 126))    for vals in itertools.product(choices, repeat=digits):        print ''.join(map(chr, vals))getCombos(3, True, True)