Dec 03, 2024 | 467 words | 5 min read
5.3.2. Task 2#
Learning Objectives#
Create and execute simple scripts comprised of basic Python concepts; Output data from a script to the screen in Python; Apply this course’s programming standards in development of Python scripts; Modularize and comment code in Python for readability and reusability.
Task Instructions#
You may recall from your high school physics class that a resistor is an electrical component that reduces the current across a circuit. Most generally, a resistor turns electrical current into a different type of energy, such as noise, light, or heat. Anything that draws current from the circuit, such as a light, can be modeled as having a resistance value R (with unit Ohm [\(\Omega\)]), and the equivalent resistance of a circuit can be calculated using the individual resistances of all components drawing current in the circuit.
There are two types of electrical circuits: series and parallel. For each of the network types one can calculate a total resistance for the entire network, but the two types are calculated differently. The following equations show how one can calculate total resistance for series and parallel circuits:
It is useful to calculate the total resistance for a circuit, as given some voltage (such as from a battery), the total current draw of the circuit can be easily calculated.
For this assignment, you will need to write a Python script (filename: py1_ind_2_username.py) using the ENGR133_Python_Template.py
to calculate the total resistance of resistors arranged in series or in parallel. The program should output the results for both series AND parallel calculations. You have been given a test case to illustrate the computation which you can use to test if your program outputs correct values.
Type of Circuit |
Resistance of Resistor 1 |
Resistance of Resistor 2 |
Total Resistance |
---|---|---|---|
Parallel |
30.0 \(\Omega\) |
10.0 \(\Omega\) |
7.5 \(\Omega\) |
Series |
30.0 \(\Omega\) |
10.0 \(\Omega\) |
40.0 \(\Omega\) |
Next, you must demonstrate the correctness of your program by applying it to the test case where the first resistance value is an input from the user and the second resistance value is \(e^2\sqrt[]{7} \text{ } \Omega\). Note that here, \(e\) is Euler’s number. Do not use the input()
function except to get the first resistance from the user. To better display your logic, create a flowchart and save it in a PDF file named py1_ind_2_username.pdf.
Sample Output#
Use the values in Table 5.10 below to test your program.
Case |
Resistor 1 |
---|---|
1 |
10 |
2 |
30 |
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 py1_ind_2_username.py Input the resistance of the first resistor [Ω]: 10 Type First Second Total Resistance Parallel 10.0 Ω 19.5 Ω 6.6 Ω Series 10.0 Ω 19.5 Ω 29.5 Ω
Case 2 Sample Output
$ python3 py1_ind_2_username.py Input the resistance of the first resistor [Ω]: 30 Type First Second Total Resistance Parallel 30.0 Ω 19.5 Ω 11.8 Ω Series 30.0 Ω 19.5 Ω 49.5 Ω
Hints
Use f-strings to format your output. Entering
\u2126
in a string will produce the \(\Omega\) symbol.To get your values to line up nicely, set a width for your f-string. For example,
f"{value:10.2f}"
will format the value to have a width of 10 characters and 2 decimal places.