Skip to main content

challenge_managing_samples_solution.py


"""
Challenge: Managing sample measurments
Solution by Lauren J Gregoire
Python programming NERC course
Useage: The program generates a random sequence of sample numbers
and prompt you to input the measurement value for that sample.
At the end, it prints out the list of values.
Date: 12/01/2013
compatibility: works with Python v 2.7
"""
# we're using the shuffle function from the random package
# shuffle modifies the order of a sequence
from random import shuffle
#1. create the list to store the results
N=5
results=[None]*N
#2. generate the random sequence of samples
random_sequence=range(N)  # generate the sequence of sample numbers (0 to N-1)
shuffle(random_sequence)  # shuffle the sequence
#3.going through the random sequence...
for sample in random_sequence:
    #a. return the sample number to measure
    print 'Measure sample',sample+1   # sample+1 goes from 1 to N
    #b. request the measurement obtained
    string=raw_input('What is the value for sample '+str(sample+1)+'?')
    #c. if meansurment isn't empty, add it to the list fo results
    if string: # an non-empty string is equivalent to True
        results[sample-1]=float(string)  # convert string to float with float() or eval()
#4. print out the results
print results