Skip to main content
Engineering LibreTexts

13.4: Assignment, Part One

  • Page ID
    26340
  • \( \newcommand{\vecs}[1]{\overset { \scriptstyle \rightharpoonup} {\mathbf{#1}} } \) \( \newcommand{\vecd}[1]{\overset{-\!-\!\rightharpoonup}{\vphantom{a}\smash {#1}}} \)\(\newcommand{\id}{\mathrm{id}}\) \( \newcommand{\Span}{\mathrm{span}}\) \( \newcommand{\kernel}{\mathrm{null}\,}\) \( \newcommand{\range}{\mathrm{range}\,}\) \( \newcommand{\RealPart}{\mathrm{Re}}\) \( \newcommand{\ImaginaryPart}{\mathrm{Im}}\) \( \newcommand{\Argument}{\mathrm{Arg}}\) \( \newcommand{\norm}[1]{\| #1 \|}\) \( \newcommand{\inner}[2]{\langle #1, #2 \rangle}\) \( \newcommand{\Span}{\mathrm{span}}\) \(\newcommand{\id}{\mathrm{id}}\) \( \newcommand{\Span}{\mathrm{span}}\) \( \newcommand{\kernel}{\mathrm{null}\,}\) \( \newcommand{\range}{\mathrm{range}\,}\) \( \newcommand{\RealPart}{\mathrm{Re}}\) \( \newcommand{\ImaginaryPart}{\mathrm{Im}}\) \( \newcommand{\Argument}{\mathrm{Arg}}\) \( \newcommand{\norm}[1]{\| #1 \|}\) \( \newcommand{\inner}[2]{\langle #1, #2 \rangle}\) \( \newcommand{\Span}{\mathrm{span}}\)\(\newcommand{\AA}{\unicode[.8,0]{x212B}}\)

    The concept of quality control is very important. Suppose you work for a company that makes resistors. The resistors would have to be tested on a regular schedule to ensure that they are within appropriate manufacturing tolerances. A few hundred items might be pulled from a batch and measured. The sample would then be analyzed statistically and compared to the standard. If it meets the standard, all is good. If it doesn’t meet the standard, the process needs to be investigated to find the source of the error(s). The assignment will mimic this. A group of measured resistors will be analyzed by the program. The results will include the number of resistors tested, the maximum and minimum values found, the arithmetic mean (i.e., average), the median (i.e., the value such that 50% of the sample is larger and 50% is smaller), the percentage of parts that fall outside the lower and upper tolerance limits, the percentage of parts that fall within the lower tolerance limit and the nominal, and the percentage of parts that fall within the nominal and the upper tolerance limit.

    This is a fair amount of analysis and will require some testing to make sure that it works properly. The program will be split into two versions. The first will focus on the statistical analysis using a small number of resistors that will be entered manually. The second version will replace the manual entry with access to a file that contains a large number of resistor values (possibly thousands). While it’s possible to do both at the start, splitting it will make it easier to find possible errors.

    Let’s start with a pseudo code, we’ll use R for \(r\)esistor values and N for \(n\)umber of items:

    1. Establish list for resistors, R, and four variables to keep track of the number of resistors. found in the four tolerance zones (Ntoolow, Nlow, Nhigh, Ntoohigh).

    2. Ask the user for the number of resistors being tested: N.

    3. Ask the user for the nominal value and tolerance: Rnom, Tol.

    4. Start a loop that will run for as many resistors as stated.

    5. Ask the user for a resistor value.

    6. Append the resistor to the list, R.

    7. End of step 4 loop.

    8. Determine the upper and lower tolerance limits: Rlowerlimit, Rupperlimit.

    9. Determine actual number of resistors entered.

    10. Sort the list.

    11. Determine the largest and smallest resistor values: Rmax, Rmin.

    12. Determine the mean and median values: Rmean, Rmedian.

    13. Start a loop that will cycle through the list, R.

    14. Compare the current item in the list to the lower and upper limits, and the nominal to determine which of the four zones the resistor falls within, and then increment the appropriate region variable.

    15. End of step 13 loop.

    16. Turn the zone totals into percentages.

    17. Print out the total number of parts tested, the largest and smallest resistors, the mean and median values, and the four percentages.

    Now for some Python code. Step one is basic initialization. The four zone totals are set to zero while the list is initialized as empty. Note the multiple assignment shortcut in the second line:

    R=[]
    Ntoolow = Nlow = Nhigh = Ntoohigh = 0.0
    

    Steps two and three should be familiar input() statements with int() or float() as appropriate. Steps four through seven are perfect for a for loop:

    for x in range(N):
         rn = float(input(“Enter the next resistor value: ”))
         R.append(rn)
    

    Step eight should be familiar from prior work, simply using Rnom and Tol to determine the upper and lower tolerance limits for the resistor, namely, Rlowerlimit and Rupperlimit. The total number of parts entered (step 9) may be found via the len() function, and then sorted (step 10) via the sort() method:

    N = len(R)
    R.sort()
    

    Strictly speaking, N is already known and the first line is not needed, however, this will be needed for part two so we might as well add it here. Once the list has been sorted, the largest and smallest resistors will be located in the first and last positions of the list (step 11):

    Rmin = R[0]
    Rmax = R[N-1]
    

    To determine the mean and median values in step 12 we’ll call a couple of functions (yet to be written):

    Rmean = find_mean(R)
    Rmedian = find_median(R)
    

    For steps 13 through 15, a for loop is again ideal, this time with a twist:

    # As R is a list, rn will cycle through all of R’s values
    # automatically, from first to last
    for rn in R:
         if rn < Rlowerlimit: # out of tolerance on low side
              Ntoolow += 1
         elif rn < Rnom: # must be between lower limit and nominal
              Nlow += 1
         elif rn <= Rupperlimit:
              Nhigh += 1
         else:
              Ntoohigh += 1
    

    Note that by “stacking” the range tests with elif, we don’t have to explicitly test for both “edges” of the middle zones. Step 16 is relatively straightforward. To turn any total into a percentage, just divide by the total number of resistors and multiply by 100 (note that the four N values were initialized as floats so there isn’t an integer divide problem here). For example, to introduce new percentage variables:

    Ptoolow = 100.0 * Ntoolow / N
    

    The step 17 print out(s) merely need to be arranged to make the output report clear and logically presented.

    Time to look at those two functions: They make writing the code easier in that we think “top down”, that is, the big picture first. Now we have to drill down into the detail. The code for determining the mean is fairly easy, simply sum up all the values and divide by the total:

    def find_mean( q ):
         tot = 0.0
         c = len(q)
         for x in range(c):
              tot += q[x]
         return tot/c
    

    A slightly more compact version is:

    def find_mean( q ):
         tot = 0.0
         for rx in q:
              tot += rx
         return tot/len(q)
    

    Finding the median is a bit trickier. The following will only work with a list that has already been sorted (note steps 10 and 12 in the pseudo code). The trick is to find the middle-most occurrence in the list. If it’s an odd sized list that’s a single value but if it’s an even sized list we’ll need to average the middle two. In either case, we’ll need to find the size of the list and cut that in two in order to get to the middle occurrence. Remember, when accessing any member of a sequence (such as our list), the index must be an integer. The int() function will be useful for this.

    def find_median( q ):
         c = len(q)
         if c%2:   # remainder means c is odd
              return float(q[int(c/2)])
        else:      # it’s even, average the two middle values
              c /= 2
              return (q[int(c)]+q[int(c-1)])/2.0
    

    Before looking at part two, make sure that the code above is operating correctly. It is suggested that you name it ResTolManual.py. Try the program with a couple small sets of resistors that you can test quickly by hand. Make sure you try both even and odd sized sets and sets with all legal resistor values, no legal values, and a mix of values. Only when you are satisfied that the program works should you head to part two.


    This page titled 13.4: Assignment, Part One is shared under a not declared license and was authored, remixed, and/or curated by James M. Fiore.

    • Was this article helpful?