Apr 28, 2026 | 357 words | 4 min read
12.1.2. Task 0#
Learning Objectives#
Create simple user-defined functions 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.
Introduction#
Python's math library provides many mathematical functions and
constants that are useful for computational tasks. You can explore the full
math library documentation.
Understanding how to use these built-in functions will enhance both your problem-solving
skills and your ability to contribute effectively to future assignments.
Before beginning, be sure to familiarize yourself with the pre-class materials.
Task Instructions#
In this task, you will be writing a UDF to perform specific calculations in
Python. Start by making a copy of the ENGR133_Python_Template.py Python template and renaming it
as py2_pre_0_username.py. Make sure
to fill out all header information, including a short description of the code.
Include all needed
importstatements in the labeled section of the template.Create a function called
calc_performbelow yourimportstatements that takes in three input arguments (\(a\), \(b\), and \(c\) in that order) and performs the following calculations based on a conditional test on input \(a\). The conditional should consist of oneif-elsestatement. If \(a \gt 4\), perform calculation (12.1); otherwise perform calculation (12.2).(12.1)#\[\frac{a^2 + \cos b - \ln c}{b - a \cdot c}\](12.2)#\[\frac{\sqrt{a + b}}{c! + \sin b}\]Use a single return statement to return the answer to the selected equation.
Hint
Ensure that each equation within the
iforelsestatements is assigned to the same variable. Then use a single return statement at the end of the function that returns this variable.In your main script, initialize
aas user input,bto \(135\) andcto \(3\). Then call yourcalc_performfunction and print its result as a single formatted string (f-string) statement to two decimal places. Your output should match the formatting shown in the Sample Output section below.Hint
Review the Python 1 materials if you need a refresher on how to obtain user input.
Save your program as py2_pre_0_username.py and turn in the assignment on Gradescope.
Sample Output#
Use the values in Table 12.2 below to test your program.
Case |
a |
|---|---|
1 |
5 |
2 |
4 |
3 |
-0.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_pre_0_username.py Input a number for variable a: 5 The result of the function was 0.19
Case 2 Sample Output
$ python3 py2_pre_0_username.py Input a number for variable a: 4 The result of the function was 1.94
Case 3 Sample Output
$ python3 py2_pre_0_username.py Input a number for variable a: -0.3 The result of the function was 1.91
Deliverables |
Description |
|---|---|
py2_pre_0_username.py |
Your completed Python code. |