\[ \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{{\kern-4mu}\%}} % % Angle \newcommand\radian{\unit{rad}} \newcommand\degree{\unit{{\kern-4mu}^\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}} \newcommand\pound{\unit{lbs}} % % Temperature \newcommand\kelvin{\unit{K}} \newcommand\K{\kelvin} \newcommand\celsius{\unit{{\kern-4mu}^\circ C}} \newcommand\C{\celsius} \newcommand\fahrenheit{\unit{{\kern-4mu}^\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 % % Capacitance \newcommand\farad{\unit{F}} \newcommand\F{\farad} \newcommand\microfarad{\micro\farad} \newcommand\muF{\micro\farad} % % 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} \]

Apr 28, 2026 | 561 words | 6 min read

12.2.1. Task 1#

Learning Objectives#

Create simple UDF’s and conditional statements in Python. Practice writing a UDF in the same file as the main script and calling the respective function in the main script.

Task Instructions#

Run each team member’s Task 0 code and compare the results with the given solutions. Discuss the differences and similarities of your approaches, highlighting strengths of each.

Reflection#

Open the Reflection Template py2_team_1_teamnumber.docx, and save it with your team’s number replacing teamnumber in the file name. Once completed, save the document as a PDF named py2_team_1_teamnumber.pdf.

Also, choose one team member’s working code file as the basis for the following reflection questions. Save a copy of the selected file as py2_team_1_teamnumber.py, again replacing teamnumber in the file name with your team’s number.

Answer the following questions in the reflection:

  1. What similarities did you notice between the team’s codes? What differences?

  2. Did everyone get the same results? If not, why?

  3. Did everyone use UDFs? If not, why?

  4. Did everyone use conditional statements? If not, why?

  5. Who wrote the most efficient/shortest code? Why do you think that is?

  6. For each team member, rank your comfort level in the following skills from \(1\) (not comfortable) to \(5\) (most comfortable).

    1. Creating UDFs in Python

    2. Implementing nested conditional statements

    3. Returning calculated output from a UDF

  7. Which working code did the team decide to submit and why? What are the strengths of this approach?

Code Modification#

Modify your py2_team_1_teamnumber.py as described below:

  1. Convert all inputs a, b, and c to be user inputs using the input() function.

  2. Change the UDF to take a single dictionary as an argument instead of individual variables. The dictionary should have keys ‘a’, ‘b’, and ‘c’ corresponding to the respective input values. You may want to review how to create and use dictionaries in Python from Section 11.1.1.

    Note

    Recall that when your code typically calls a function, you need to provide a way for values from the function to be returned if you want to use them in the main script. Passing a dictionary to a function means you can modify the values within the dictionary inside the function, and those changes will be reflected outside the function as well because dictionaries are mutable objects in Python.

    A dictionary also allows you to pass multiple values without needing to return each one individually. This is especially useful when there are many values to pass, or when the number of parameters may change. Using a dictionary can help keep your code organized and easier to read by simplifying the function signature.

    Note that accidentally changing a mutable function argument, like the values in a dictionary, is a common source of bugs in Python code. So be sure to test your code thoroughly after making these changes.

  3. Call the UDF from your main script, passing in a dictionary with the user-provided values for ‘a’, ‘b’, and ‘c’.

  4. Ensure that the UDF still implements the conditional logic based on the value of ‘a’ and calculates the output accordingly using the same formulas as before.

Sample Output#

Use the values in Table 12.4 below to test your program.

Table 12.4 Test Cases#

Case

a

b

c

1

5

10

17

2

4

11

7

3

3

12

0

4

4

135

3

Ensure your program’s output matches the provided samples exactly. This includes all characters, white space, and punctuation. In the samples, user input is highlighted like this for clarity, but your program should not highlight user input in this way.

Case 1 Sample Output

$ python3 py2_team_1_teamnumber.py Input a number for variable a: 5 Input a number for variable b: 10 Input a number for variable c (must be a non-negative integer): 17 The result of the function was -0.28

Case 2 Sample Output

$ python3 py2_team_1_teamnumber.py Input a number for variable a: 4 Input a number for variable b: 11 Input a number for variable c (must be a non-negative integer): 7 The result of the function was 0.00

Case 3 Sample Output

$ python3 py2_team_1_teamnumber.py Input a number for variable a: 3 Input a number for variable b: 12 Input a number for variable c (must be a non-negative integer): 0 The result of the function was 8.36

Case 4 Sample Output

$ python3 py2_team_1_teamnumber.py Input a number for variable a: 4 Input a number for variable b: 135 Input a number for variable c (must be a non-negative integer): 3 The result of the function was 1.94

Table 12.5 Deliverables#

Deliverables

Description

py2_team_1_teamnumber.pdf

The completed team reflection document.

py2_team_1_teamnumber.py

The modified Python code file.