''' Purpose: Experience the thrill of random generation and list examination Task: Prompt and get three integers s, n, and d from its user (in that order). Use integer s as a seed to the Python random number generator. Then print n octal digits (base 8 digits) line by line by line. Then print the number of generated occurrences of d. There should be no other output Checker: ''' # needed module import random # Get the input reply = input( 'Enter three numbers: ' ) print() # Convert the input into the integers s, n, d s, n, d = reply.split() s, n, d = int( s ), int( n ), int( d ) # s = seed # n = how many numbers we need to generate # d = value we're interested in --> need to find how many times this value occurs # in a list of n octal numbers # octal numbers = numbers in range 0 - 7 # Set the seed for generating random values random.seed( s ) # when we set the seed, the machine still chooses random things # but we get the same random choices will occur every time we run the program # so it's kinda predictable randomness (at least after the 1st time you run the program) # Generate n random octal numbers and store them in a list # Start by initializing the list holder octals = [] # One-by-one add n octal numbers to the list holder for i in range( 0, n ) : # each time through, generate a random octal number and add to octals list digit = random.randrange( 0, 8 ) # range = [0, 8) - octals = base 8 number; includes 0 through 7 # since we set a seed, we will get the same list every time we run if we give it the same seed # if s is a different thing, our list will be different # to use random.choice( x ), we need a list: x = [0, 1, 2, 3, 4, 5, 6, 7] --> too long to type # using randrange() is better octals.append( digit ) # Print the list print( "octal digit list:", octals ) print() # Determine number of generated octal values equal to d and print # how many times d appears in the list octals number_of_occurrences_of_d_in_octals = octals.count( d ) print( "number of times value", d, "in list:", number_of_occurrences_of_d_in_octals )