Skip to main content
Engineering LibreTexts

13.5: Assignment, Part Two

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

    In this section we shall modify the program from part one in order to utilize external data files. The data file will have a very simple structure. The first item will be the nominal value. The second line will contain the percent tolerance. After that, each line will contain a single measured resistor value. So, a file with three resistors, nominally 10 k ohm and 5 percent might look like:

    10000.0
    5.0
    10234.1
    9978.5
    9863.2
    

    Much of the earlier code will be reused. Generally, anything that required a input() statement will be replaced by a readline() method. In fact, there will be only one input() statement, and that’s to get the name of the data file from the user. So, instead of asking for the number of resistors, the nominal value and percent tolerance, instead we ask for the filename and then open the file in read mode:

    fn = input("Enter name of resistor file to be processed: ")
    fil = open( fn, "r" )
    

    Once the file is opened we can read the first two lines which are the nominal value and percent tolerance:

    Rnom = float( fil.readline() )
    Tol = float( fil.readline() )
    

    The main resistor input loop needs to be reorganized. We no longer know how many resistors are in the data file. While it’s possible to ask the user to count them (a tedious waste of time), using a while loop can solve the problem. If we call readline() and there are no more lines to be read (i.e., we’ve already read the final resistor in the file), readline() will return a null (empty) string (“” with no internal space). Think of the loop as “going until there are no more resistors”. We establish a variable, keepgoing, as True, and reset it to False if we get back a null string (that is, nothing more to read). If we do get something, we turn it into a float and append it to the resistor list. Once the loop terminates we close the file:

    keepgoing = True
    while keepgoing:
         s = fil.readline()
         if s == "":
              keepgoing = False
         else:
              R.append( float(s) )
    
    fil.close()
    

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

    • Was this article helpful?