Dec 03, 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 theseed
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 thelimit_denominator
function do, and why might it be useful?From the
math
module, what range of values will theasin()
function accept?From the
math
module, what does theexp
function do?From the
math
module, what does thesqrt
function do and what range of values will it accept?From the
math
module, what do thedegrees
andradians
functions do?What does the built-in
input()
function do?What inputs does the built-in
round()
function take?What do the
int()
andfloat()
built-in functions do?
Save your document as a PDF file (By going to
).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.
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.