-1

I want to randomize the answers in my for loop. My current code only prints: AAA,BBB,CCC.

while True:
    for i in range(65,91):
        answer = chr(i) + chr(i) + chr(i)
        print(answer)

How can I randomly return every single possible combination like AXY,BMK, etc.

Is this possible with the random module or do I need to use something else?

VintageMind
  • 65
  • 2
  • 9
Nick
  • 3
  • 3
  • Hey there! Put answer = chr(i) + chr(i + 1) + chr(i +2). Hope I helped and happy coding! – VintageMind Sep 17 '21 at 07:53
  • Your answer is working however because of the for loop it only returns items 26 times because of the 65,91 range. How can i solve that? – Nick Sep 17 '21 at 07:56

1 Answers1

0

Try this:

import string
import random
char_big = string.ascii_uppercase

while True:
    print(''.join(random.choice(char_big) for _ in range(3)))

By thanks of @JiříBaum, if this is important for you to prevent from attack you can use SystemRandom from secrets like below:

import string
import secrets
char_big = string.ascii_uppercase

while True:
    print(''.join(secrets.choice(char_big) for _ in range(3)))

Or:

import string
import secrets
char_big = string.ascii_uppercase

while True:
    print(
        ''.join(char_big[secrets.SystemRandom().randrange(0, 26)] \
                for _ in range(3)
               )
    )
I'mahdi
  • 23,382
  • 5
  • 22
  • 30