Skip to main content
Engineering LibreTexts

7.2: Assignment

  • Page ID
  • \( \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 our battery life estimator program. It is suggested that you name this program The goal is to create a simple utility where the user can select a battery from a list of choices, specify a current draw and have the program print out the expected lifespan of the battery. This hinges on the concept of battery amp-hour ratings. That is, the expected life of a battery in hours is equal to its amp-hour rating divided by the current draw in amps. The amp-hour rating depends on the size of the battery and its chemical composition (for example, D cells are higher than C cells and alkaline cells are higher than zinc-carbon cells). Here are some examples in amp-hours:
















    Table \(\PageIndex{1}\)

    The expected life is simply the desired amp-hour rating above divided by the current draw:

    life = amphours / I

    Here is an appropriate pseudo code for the program:

    1. Print out program description and user directions.

    2. Ask user for the current draw.

    3. Print out the battery choice list.

    4. Ask the user to choose a battery from the list.

    5. Look up the corresponding amp-hour rating for the chosen battery.

    6. Compute the expected battery life.

    7. Print out the expected battery life.

    Some of these lines we have already seen. For example, the “Ask” directives of steps two and four imply the use of input() statements. For example, for step two we might use:

    I = float(input(“What is the current draw in amps? ”))

    The printouts and computations of steps one, six, and seven should also be familiar (you may find the prior time printout example to be useful here). Step three is really nothing more than either a set of print statements or a single print statement using a triple quoted string to display all of the battery choices. It is safest to present the menu of battery choices numerically as it is easy for the user to select an item by simply typing in a number versus spelling out the entire description of the battery (it turns out to be easier for the programmer, too). For example, the first few statements of this step might look like:

    print( “Please choose a battery from the list below” )
    print( “1. AAA zinc-carbon” )
    print( “2. AAA alkaline” )
    print( “3. AA zinc-carbon” )
    # and so forth

    Step four would then look something like:

    battery = int(input(“Please enter the battery choice number, 1 
    through 8: ”))

    The trick is step five, looking up the corresponding amp-hour rating. For this step a series of if statements may be used:

    if battery == 1: # AAA zinc-carbon
         amphours = 0.2
    if battery == 2: # AAA alkaline
         amphours = 0.5
    if battery == 3: # AA zinc-carbon
         amphours = 0.4
    if battery == 4: # AA alkaline
         amphours = 0.8
    # and so forth

    There are other ways to structure this “chain of choices” as we shall see, but this technique is perfectly serviceable for now. Along with your code, hand in two trial runs: The first should be a zinc-carbon D cell with a 0.1 amp draw and the second should be a AAA alkaline cell with a 0.6 amp draw.

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

    • Was this article helpful?