Nov 16, 2026 | 524 words | 5 min read
9.1.3. PCA: User-Defined Functions#
Engineers often use functions to organize calculations into reusable components. In this activity, you will create a user-defined function to calculate the power dissipated by a resistor in an electric circuit.
Ohm’s Law \((V = I*R)\) relates voltage (V), current (I), and resistance ® in electric circuits. Using this, the power dissipated by a resistor in a circuit can be calculated using the equation below:
Where:
\(P\) is power measured in watts (W)
\(V\) is voltage measured in volts (V)
\(R\) is resistance measured in ohms (\(\Omega\))
Files Needed#
For this assignment, download the Python template and
save as 9_1_3_username.py.
Reminder
Rename your files with your Purdue username by replacing “template” in the filename. Then fill out any required information in the template, such as your name, email, and course details.
Review and reference the Python Style Guide to create a professionally formatted code file.
Instructions#
Write a user-defined function that calculates the power dissipated by a resistor using the equation provided above.
Function name: `calc_power`
Parameters: voltage (`V`) and resistance (`R`)
Return value: power (`P`)
Then, in the main() function, write code that does the following:
Prompt the user to enter the voltage and resistance.
Call
calc_power()using the values entered by the user.Store the value returned by
calc_power().Display the calculated power in watts (W), formatted to two decimal places.
Test your Python code using the sample input/output below.
Sample Output#
Use the values in Table 9.4 below to test your program.
Case |
Voltage (V) |
Resistance (ohms) |
|---|---|---|
1 |
5 |
150 |
2 |
12 |
625 |
3 |
10 |
1000 |
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
Case 2 Sample Output
Case 3 Sample Output
Gradescope Submission#
Save Deliverables: Ensure all required assignment files are saved and named exactly as specified in the assignment instructions. If a template file name contains the word
template, replace only that word with your Purdue username. When a task lists more than one deliverable, such as a workbook and a report, submit each deliverable to its own matching Gradescope assignment instead of combining them into one upload.Navigate to Gradescope: Log in to Gradescope. From your Dashboard, open the assignment.
Upload your work: Select the Upload submission method. Drag and drop all required files (or a compressed
.zipfolder) into Gradescope, and select Upload.Confirm Submission: When your upload is successful, you will see a confirmation message on your screen and receive an automated email receipt.
Add Team Members (if applicable): If this is a team assignment, add all team members to the submission. Each teammate should confirm they can see the submission in their own Gradescope account. For help, visit Gradescope’s documentation on adding team members.
Review Autograder Results: Wait a few minutes for the autograder to evaluate the relevant files. Scroll down to view any failed test cases. If you need assistance interpreting the output or errors, ask your teaching team.
Resubmit if Necessary: You can edit your files and select Resubmit as many times as you need before the assignment deadline. Only your final, active submission is graded. (Note: If you need to resubmit, you must upload all required files for the assignment again).
For a detailed walkthrough, review the official Gradescope Submission Guide.