How can I print all possible answers in python?

There are 7 questions with 4 choices ( A, B, C, D). I want to log or print all possible answers. For example

> A A A A A A A
  A A A A A A B
  A A A A A A C
  A A A A A A D
  B A A A A A A
  B A A A A A B
  B A A A A A C
  D D D D D D D

How can I print it?

I’ve tried the following

for a in range(7):
    for b in range(7):
        for c in range(7):
            for d in range(7):
                print(a, b, c, d)

But this outputs

> 1 1 1 1
  1 1 1 2
  1 1 1 3
  1 1 1 4
  6 6 6 6

But i need to get 7 possible answers to be printed on each line.

>Solution :

This should solve your problem:

from itertools import product as prod
list(prod('abcd', repeat=7))

If you wanted your code to work then you would have to have 7 for loops for 7 questions but this is not a great way of solving this because what if I told you there are 100 questions? are you gonna implement 100 for loops?

For these types of scenarios there are usually built in functions or other libraries such as itertools. If you wanted to implement it yourself then you would probably do it recursively

Leave a Reply