''' Purpose: construct list of n base 2 digits for a user-supplied n ''' import random # want random values, then we need the random module # get the number of wanted bits reply = input( 'How many bits: ' ) n = int( reply ) print() # create an empty list of n bits bits = [] # start off with an empty list # add n random 0/1's to bits for i in range( 0, n ) : # i is the iterator, loop will repeat n times # each iteration want to add another bit to the list of bits # get the next bit bit = random.randrange( 0, 2 ) #range from zero up to but not including 2 bit = random.choice( [0, 1] ) #need to give it a list, so you can create the #list prior to calling on choice or put the list within the brackets # append the bit to the list of bits so far bits.append( bit ) #sticks the digit on the end of the list #print bits print(bits) #when you have a print statement in a loop, it will print what #the list currently contains every time the loop runs #if you put the print statement outside of the loop, it will just print the #final product of the list