\[ \begin{align}\begin{aligned}\newcommand\blank{~\underline{\hspace{1.2cm}}~}\\% Bold symbols (vectors) \newcommand\bs[1]{\mathbf{#1}}\\% 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} \]

Oct 24, 2024 | 690 words | 7 min read

5.2.2. Task 2#

Learning Objectives#

Perform arithmetic operations in Python (i.e., addition, subtraction, multiplication, division, and exponentiation), while keeping in mind order of operations; Employ VS Code to write, edit, and save Python code; Output Python data from script to screen; use various built-in and imported functions; import modules into script.

Introduction#

As you further explore programming, you will find that there are several concepts and features hidden within each language that lend greatly to the usability of the language in a niche field or when looking to solve a specific problem in a specific way.

Python contains several elements within its built-in modules that apply to unique situations or tasks. In this activity, you will be asked to delve deeper into the world of Python modules. To learn about and apply these modules, you will need to do some research on your own. Being able to use your resources to help you find answers to difficult problems will be an important skill for you to master, not only for this class, but also going forward as an engineer.

Part A: Finding and using Python documentation#

Open a MS Word document and name it as py1_team_2_part_a_teamnumber.pdf. Either use the internet or type help function_name (e.g., help(round)) into the Python console to answer the following questions. Also, not all questions should be answered by the same student, so the name of the student who answered that question should be listed. Each student in the team should answer at least 1 of the questions.

  • For the random module, what does the seed function do, and why would you want to set a seed?

  • What function in the random module would you use to generate a random number between some value, a, and another value, b? Provide an example of a command that would give a random number between \(5\) and \(200\).

  • From the fractions module, what does the limit_denominator function do, and why might it be useful?

  • From the math module, what range of values will the asin() function accept?

  • From the math module, what does the exp function do?

  • From the math module, what does the sqrt function do and what range of values will it accept?

  • From the math module, what do the degrees and radians functions do?

  • What does the built-in input() function do?

  • What inputs does the built-in round() function take?

  • What do the int() and float() built-in functions do?

Save your document as a PDF file (By going to File ‣ Print ‣ Microsoft Print to PDF).

Investigate the difference between the following and how it affects the calling of functions:

from <module> import <function>
import <module>

Part B: Using these modules in a script#

You are spending your next summer consulting at a start-up company that creates on-line educational tools to assist elementary school students in practicing math. You are working on a tool that demonstrates to students the equivalence between doing calculations with decimal numbers and doing the same calculations with fractions. The tool should randomly generate four numbers as follows:

  • The first number must be between \(0\) and \(100\).

  • The second number must be between \(10\) and \(50\).

  • The third number must be between \(20\) and \(40\).

  • The fourth number must be between \(100\) and \(200\).

Add the numbers together, and then convert these numbers to fractions and repeat the addition using the fractions. The random numbers should all be rounded to \(3\) decimal digits, as should the sum of these numbers. The fractions should be printed to the screen with a denominator smaller or equal to \(100\). An example output is shown below.

Open the template, ENGR133_Python_Template.py, and rename it as py1_team_2_teamnumber.py that will follow the procedure outlined above. Utilizing random.seed(int(input("Enter the seed: "))) before generating the random numbers initially is required in your program for it to be graded properly.

Sample Output#

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

Table 5.4 Test Cases#

Case

Seed

1

5

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_team_2_teamnumber.py Enter the seed: 5 First Random Number : 62.29 Second Random Number : 39.671 Third Random Number : 35.904 Fourth Random Number : 194.245 Sum from decimals: 62.29 + 39.671 + 35.904 + 194.245 = 332.11 Sum from fractions: 6229/100 + 3015/76 + 2621/73 + 9518/49 = 33211/100

Before you start programming in Python, create a flow chart representing the algorithm and save it in a PDF file that will be named py1_team_teamnumber.pdf.

Note that your program will be graded based upon the output and the process that you used to reach this output. Your program should not produce any errors when running or points will be deducted. Do not use the input() function except to set the seed for the pseudo-random number generator.