Printing out a random string (Python)
Question: I am asked to produce a random string using a previously defined value.

    
    
    THREE_CHOICES = 'ABC'
    FOUR_CHOICES = 'ABCD'
    FIVE_CHOICES = 'ABCDE'
    
    import random
    
    def generate_answers(n: int) -> str:
        '''Generates random answers from number inputted '''
        for i in range(0,n):
            return random.choice(FOUR_CHOICES)
            continue
    

What I am trying to get happening here is, if I were to plug in 7 into the
function I'd get something like "ABDCBBA"

A random 7 character string from the specified letters. However, this code is
returning only one random character no matter what number I put in (has to be
non-zero).

I don't know what to do here, can someone point me in the right direction?


Answer: you could just return a list like this:

    
    
    def generate_answers(n):
        '''Generates random answers from number inputted '''
        randoms = [random.choice(FOUR_CHOICES) for _ in range(0,n)]
        return randoms
        #return ''.join(randoms) to get string
    
    print generate_answers(7)  
    

this would print a list like this: `['D', 'D', 'D', 'B', 'A', 'A', 'C']`

you could `join` the list if you want a string

