Skip to main content
Engineering LibreTexts

6.1: Introduction

  • 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}}\)

    The most general means of obtaining information from the user is the input() function. When Python executes this command it will wait for the user to enter a string of characters until the user hits the Enter key. These characters are then assigned as a string to a variable. Usually, some form of user prompt will be required (i.e., the question posed to the user). This can be accomplished via a separate print statement or as an argument to the function. Below is a simple example which asks the user for their name and then prints it back out.

    print( “What is your name? ” )
    n = input()
    print( “Hello”, n )

    Alternately, this can be shortened with the following:

    n = input(“What is your name? ”)
    print( “Hello”, n )

    It is important to remember that this function always returns a string variable. If the entered data is numeric, it must be turned into either a float or integer. This can be accomplished via the float() and int() functions. For example:

    p = float(input(“What is your weight in pounds? ”))
    # one kilogram is approximately 2.2 pounds
    kg = p / 2.2
    print( “You weigh approximately”, kg, “kilograms” )

    Let’s consider how we might create a simple Ohm’s law calculator. Before we start coding, we must define exactly what we wish the program to do and create a logical outline. This outline is not written in python but rather a simplified form of English which shows the steps required to solve the problem. One line of pseudo code might correspond to one line of Python. Alternately, it might correspond to many lines of Python. Pseudo code is not tied to a specific language.

    In our example, we shall use Ohm’s law in the form V=I*R. Here’s the pseudo code:

    1. Give the user directions.

    2. Ask the user for current in amps.

    3. Ask the user for the resistance in ohms.

    4. Compute the voltage from V=I*R.

    5. Print out the resulting voltage in volts.

    Step one might be very short or very detailed. It all depends on the complexity of the program. Note that in steps two, three and five, we have specified the units. This is important. The user should not assume that it’s OK to enter a current in milliamps, for example. It is worth noting that input() can deal with an exponent so a value such as 1.2 milliamps can be entered as 1.2e-3 So here’s the Python code based on this pseudo code.

    # Programmer’s name, date, etc.
    print( “\t\tOhm’s Law Calculator\n” )
    print( “This program will determine the voltage given a current 
    and resistance.\n” )
    I=float(input(“What is the current in amps? ”))
    R=float(input(“What is the resistance in ohms? ”))
    print( “The result is”, V, “volts” )

    The \t and \n sequences are used to enter a Tab and a Newline, respectively. This makes the print out look a little nicer. Enter this program, save it as and run it. Try the values 2 amps and 10 ohms. The result should be 20 volts. Also try some large and small values, for example, test the program with a current of 3 milliamps (3.0e−3) and 5 k ohms (5.0e3). The result for this should be 15 volts. It is important to always test your code! Never assume that it runs properly without exhaustive testing.

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

    • Was this article helpful?