Creating an Algorithm to generate 20 digit codes
Question: I am wanting to create an Algorithm that creates a 20 digit number code.
Similar to how gift card codes are created. I would like to use python for it,
since I know python the best out of ALL programming languages. I would like it
to create numbers based on certain rules (For ex. Like how certain gift cards
have to start with an S and has to have X number of letters in it and numbers)
I do not know if I am explaining this clearly, but basically what I want is to
create an Algorithm that will create a 20 digit code that has certain rules so
people cannot create "false" codes. Thank you! :)

The Code I have currently is

    
    
    import random
    
    def random_with_N_digits(n):
        range_start = 10**(n-1)
        range_end = (10**n)-1
        return randint(range_start, range_end)
    
    print random_with_N_digits(20)
    

But I keep getting an error on the print statement?


Answer: An algorithm is basically a set of rules to accomplish something, start it as
you would start any piece of code. Since your question lacks some data, I will
just make an assumption that you want to randomly select a letter or number.

The algorithm will require a list of letters and numbers, and will simply
state that: for each number in range from 1-20, you want to 'run' a set of
rules.

This set of rules that you want to run is nothing more than a function or some
code that can randomly select a letter/number from your data list.

The code above will build you a data list with numbers and letters that you
can have the algorithm randomly select from.

    
    
    import string
    from random import randint
    data = list(string.ascii_lowercase)
    [data.append(n) for n in range(0, 10)]
    

The code below will range from 0 through 21 (1, 20) and will generate a 20
character string based on a random selection of a number or letter that is
inside your data list

    
    
    x = [str(data[randint(0, len(data)-1)]) for n in range(0, 21)]
    y = ''.join(x)
    print y
    >>> 'km1o07fy9t3j0dktabjsg'
    

