Skip to main content
Engineering LibreTexts

8.2: Assignment

  • Page ID
    26422
  • \( \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}}\)

    Now let’s consider a resistor tolerance program. It is suggested that you name this program ResistorTolerance.py. The goal is to create a simple utility where the user enters a nominal resistor value and tolerance (i.e., based on the color code) along with a measured resistance. The program will then determine if the measured resistor is within specs. If it is, a success message is produced, if not, a failure message is produced along with the actual tolerance. The pseudo code might look something like this:

    1. Give the user directions.

    2. Ask user for nominal and measured resistor values in ohms along with the tolerance as a percent.

    3. Compute the legal maximum and minimum resistor values.

    4. Compare the measured value to the legal max and min. If it’s within, print out a success message. If it’s outside the range, compute the actual tolerance and print it out along with a failure message.

    There are other ways of designing this program. For example, you could compute the actual tolerance immediately and then compare that to the stated tolerance to see if the resistor is within specs.

    Creating the program consists of many steps we’ve already seen. For example, giving the user directions involves simple print statements while obtaining the measured and nominal resistor values can make use of input() statements. The tolerance is specified to the user as a percentage as that’s easiest for them, however, computation requires that it be expressed as a factor. You can divide the tolerance by 100 after obtaining it but it might be easier to do it all in one step:

    tol = float(input(“Enter the percent tolerance: ”))/100.0
    

    The upper and lower limits can then be found by determining the offset in ohms and then adding and subtracting this value to/from the nominal:

    Roffset = Rnominal * tol
    Rupper = Rnominal + Roffset
    Rlower = Rnominal - Roffset
    

    We are now ready for step four. Note that in order for a resistor to be considered good, it must meet two requirements: It must be equal to or greater than the lower limit and also be equal to or less than the upper limit. The and operator is perfect for this:

    if Rmeasured >= Rlower and Rmeasured <= Rupper:
         print( "This device is within tolerance." )
    

    Conversely, the resistor could be checked to see if it’s bad. That is, we can use the or operator by reversing the sense of the test:

    if Rmeasured < Rlower or Rmeasured > Rupper:
         print( "This device is out of tolerance." )
    

    There remains the issue of computing the actual deviation of a failed device. This may be done via a simple formula:

    Actualdev = 100.0 * (Rmeasured - Rnominal) / Rnominal
    

    So, combining the elements above, step four may be performed in two basic ways. First, by checking to see if the resistor is good and second by checking to see if it’s bad:

    if Rmeasured >= Rlower and Rmeasured <= Rupper:
         print( "This device is within tolerance." )
    else:
         Actualdev = 100.0 * (Rmeasured - Rnominal) / Rnominal
         print( "The device is out of tolerance." )
         print( "The actual deviation is", Actualdev, "percent" )
    

    or

    if Rmeasured < Rlower or Rmeasured > Rupper:
         Actualdev = 100.0 * (Rmeasured - Rnominal) / Rnominal
         print( "The device is out of tolerance." )
         print( "The actual deviation is", Actualdev, "percent" )
    else:
         print( "This device is within tolerance." )
    

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

    • Was this article helpful?