\[ \begin{align}\begin{aligned}\newcommand\blank{~\underline{\hspace{1.2cm}}~}\\% Bold symbols (vectors) \newcommand\bs[1]{\mathbf{#1}}\\% Differential \newcommand\dd[2][]{\mathrm{d}^{#1}{#2}} % use as \dd, \dd{x}, or \dd[2]{x}\\% Poor man's siunitx \newcommand\unit[1]{\mathrm{#1}} \newcommand\num[1]{#1} \newcommand\qty[2]{#1~\unit{#2}}\\\newcommand\per{/} \newcommand\squared{{}^2} \newcommand\cubed{{}^3} % % Scale \newcommand\milli{\unit{m}} \newcommand\centi{\unit{c}} \newcommand\kilo{\unit{k}} \newcommand\mega{\unit{M}} % % Percent \newcommand\percent{\unit{\%}} % % Angle \newcommand\radian{\unit{rad}} \newcommand\degree{\unit{{}^\circ}} % % Time \newcommand\second{\unit{s}} \newcommand\s{\second} \newcommand\minute{\unit{min}} \newcommand\hour{\unit{h}} % % Distance \newcommand\meter{\unit{m}} \newcommand\m{\meter} \newcommand\inch{\unit{in}} \newcommand\foot{\unit{ft}} % % Force \newcommand\newton{\unit{N}} \newcommand\kip{\unit{kip}} % kilopound in "freedom" units - edit made by Sri % % Mass \newcommand\gram{\unit{g}} \newcommand\g{\gram} \newcommand\kilogram{\unit{kg}} \newcommand\kg{\kilogram} \newcommand\grain{\unit{grain}} \newcommand\ounce{\unit{oz}} % % Temperature \newcommand\kelvin{\unit{K}} \newcommand\K{\kelvin} \newcommand\celsius{\unit{{}^\circ C}} \newcommand\C{\celsius} \newcommand\fahrenheit{\unit{{}^\circ F}} \newcommand\F{\fahrenheit} % % Area \newcommand\sqft{\unit{sq\,\foot}} % square foot % % Volume \newcommand\liter{\unit{L}} \newcommand\gallon{\unit{gal}} % % Frequency \newcommand\hertz{\unit{Hz}} \newcommand\rpm{\unit{rpm}} % % Voltage \newcommand\volt{\unit{V}} \newcommand\V{\volt} \newcommand\millivolt{\milli\volt} \newcommand\mV{\milli\volt} \newcommand\kilovolt{\kilo\volt} \newcommand\kV{\kilo\volt} % % Current \newcommand\ampere{\unit{A}} \newcommand\A{\ampere} \newcommand\milliampereA{\milli\ampere} \newcommand\mA{\milli\ampere} \newcommand\kiloampereA{\kilo\ampere} \newcommand\kA{\kilo\ampere} % % Resistance \newcommand\ohm{\Omega} \newcommand\milliohm{\milli\ohm} \newcommand\kiloohm{\kilo\ohm} % correct SI spelling \newcommand\kilohm{\kilo\ohm} % "American" spelling used in siunitx \newcommand\megaohm{\mega\ohm} % correct SI spelling \newcommand\megohm{\mega\ohm} % "American" spelling used in siunitx % % Inductance \newcommand\henry{\unit{H}} \newcommand\H{\henry} \newcommand\millihenry{\milli\henry} \newcommand\mH{\milli\henry} % % Power \newcommand\watt{\unit{W}} \newcommand\W{\watt} \newcommand\milliwatt{\milli\watt} \newcommand\mW{\milli\watt} \newcommand\kilowatt{\kilo\watt} \newcommand\kW{\kilo\watt} % % Energy \newcommand\joule{\unit{J}} \newcommand\J{\joule} % % Composite units % % Torque \newcommand\ozin{\unit{\ounce}\,\unit{in}} \newcommand\newtonmeter{\unit{\newton\,\meter}} % % Pressure \newcommand\psf{\unit{psf}} % pounds per square foot \newcommand\pcf{\unit{pcf}} % pounds per cubic foot \newcommand\pascal{\unit{Pa}} \newcommand\Pa{\pascal} \newcommand\ksi{\unit{ksi}} % kilopound per square inch \newcommand\bar{\unit{bar}} \end{aligned}\end{align} \]

Feb 17, 2025 | 267 words | 3 min read

8.2.2. Task 2#

Learning Objectives#

Determine the output of a program through hand tracking if-elif-else structures and UDFs. Investigate logical operators in Python.

Task Instructions#

For the exercises below, follow the instructions and determine the output of the code. Save your answers in a PDF file named py2_team_2_teamnumber.pdf.

Exercise A (if-elif-else)#

The Python code in Listing 8.1 is typed in after defining x and y. Assume that all other variables are cleared before the commands are executed.

Listing 8.1 Conditional practice.#
if x <= 50:
    z = 4
    if y < 30:
        z = x*y
    elif y >= 100:
        z = x+y

elif y >= 60:
    if x > 80:
        z = x
    elif y > 50:
        z = y
else:
    z = z*2

print('z =', z)

What will appear on the screen when the following Python commands are executed before the code in Listing 8.1 is entered?

  1. >>> x = 40
    >>> y = 40
    
  2. >>> x = 20
    >>> y = 20
    
  3. >>> x = 55
    >>> y = 20
    

Exercise B (functions)#

Use hand tracking to answer the following questions about Python program in Listing 8.2. Check your work by running the code in Python's interactive mode.

  1. What is printed to the console window?

  2. What variables are in the global namespace, and what are their values?

Listing 8.2 Function practice.#
def myFun1(in1, in2):
    x = in1
    y = in2
    out1 = 6
    out1, out2 = myFun2(x, y)
    print(f'in1={in1:.0f}, in2={in2:.0f}, out1={out1:.0f}')
    return out1
# --------------------------------------------------------
def myFun2(in2, in1):
    a = in1*3
    b = in2*2
    out1 = a+b
    out2 = a-b
    print(f'in1={in1:.0f}, in2={in2:.0f}, out1={out1:.0f}, out2={out2:.0f}')
    return out2, out1
# --------------------------------------------------------
def myFun3():
    var1 = 1
    var2 = 2
    output = 3
    print(f'var1={var1:.0f}, var2={var2:.0f}, output={output:.0f}')
    output = myFun1(var1, var2)
    print(f'var1={var1:.0f}, var2={var2:.0f}, output={output:.0f}')

# --------------------------------------------------------
var1 = 4
var2 = 2
myFun3()
print(f'var1={var1:.0f}, var2={var2:.0f}')

Exercise C (logic operators)#

Create a table with four columns labeled: logic operations, hand calculation, Python calculation, and differences.

Consider the logic operations in Listing 8.3 assuming that a = True and b = False.

  1. Copy the logic operations to the first column of the table.

  2. Without using Python, determine the value of each logic operation by hand and record the results in the second column.

  3. Perform the same logic operations in Python’s interactive mode, and record the results in the third column.

  4. In the fourth column, explain any differences between the hand calculation and the Python calculation.

Listing 8.3 Logical operator practice.#
1a and b
2a or b
3a == b
4(a and b) == (a or b)
5a != b
6a > b
7a >= b
8a = b
Table 8.5 Deliverables#

Deliverables

Description

py2_team_2_teamnumber.pdf

Your answers for each part of this task.