Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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))

edit:
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

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading