4

I am using itertools to generate combinations, but I would like to control the output and be able to ask for the next entry:

from itertools import permutations

def getPass():
    chars = '4$5%6^7&'
    passd = ''
    for comb in permutations(chars):
       passd = ''.join(comb)     
    return passd

while(True):
    print getPass()

But am unable to find any relevant documentation. How do I do this?

Loke
  • 183
  • 2
  • 7

1 Answers1

3

You could use a generator:

from itertools import permutations

def getPass():
    chars = '4$5%6^7&'
    for comb in permutations(chars):
       yield ''.join(comb)     

for i in getPass():
    print(i)
    input('\ncontinue?')

or even a generator expression:

from itertools import permutations

def getPass():
    return (''.join(p) for p in permutations('4$5%6^7&'))

for i in getPass():
    print(i)
    input('\ncontinue?')

Output:

4$5%6^7&

continue?
4$5%6^&7

continue?
4$5%67^&

continue?
4$5%67&^

continue?
4$5%6&^7

continue?
...
Community
  • 1
  • 1
aldeb
  • 6,588
  • 5
  • 25
  • 48