4.12. Custom Modules#
There are lots of Python modules that exist for us to use. E.g. math
,
random
, time
and datetime
modules.
But if we want to write our own custom functions we can actually write our own modules! We just need to create a Python file:
module.py
where we write all of our functions. We can then import this as we would with our usual modules.
import module
To use a function in our module, we use:
module.function()
Code Challenge: Rectangle Properties
Write a module called rectangle
that can be used to calculate properties of rectangles. You should be able to import functions from you module into your main script. Your module should contain the following 3 functions.
Area specification (written in rectangle.py
)
Formula for the area of a rectangle
\(\text{area} = \text{width} \times \text{height}\)
name:
area
parameters:
width
(int
orfloat
),height
(int
orfloat
)return: the area of the rectangle (
int
orfloat
)
Perimeter specification (written in rectangle.py
)
Formula for the perimeter of a rectangle
\(\text{perimeter} = 2\times\text{width} + 2 \times \text{height}\)
name:
perimeter
parameters:
width
(int
orfloat
),height
(int
orfloat
)return: the perimeter of the rectangle (
int
orfloat
)
Diagonal specification (written in rectangle.py
)
Formula for the diagonal of a rectangle
\(\text{diagonal} = \sqrt{\text{width}^2 + \text{height}^2}\)
name:
perimeter
parameters:
width
(int
orfloat
),height
(int
orfloat
)return: the perimeter of the rectangle (
int
orfloat
)
Examples (running from main.py
)
import rectangle
print(rectangle.area(2, 10))
print(rectangle.perimeter(5, 7))
print(rectangle.diagonal(3, 4))
20
24
5.0
Solution
Solution is locked
Code Challenge: Intergalactic
Write a program that calculates an object’s weight in Newtons on the surface of:
Earth, and
another planet,
to four decimal places.
Your project must consists of two files:
intergalactic.py
- takes care of input/output and running the programphysics.py
- holds functions for the calculations
Acceleration specification (written in physics.py
)
Formula for acceleration due to gravity on a planet’s surface
\(a = \frac{G m}{r^2}\)
where \(G = 6.67430\times10^{-11}\) is the gravitational constant and \(m`\) and \(r`\) are the mass and radius of the planet respectively.
name:
acceleration
parameters:
planet_mass
(float
expected - kg),planet_radius
(float
expected - m)return: acceleration due to gravity on the planet’s surface (
float
)
Weight specification (written in physics.py
)
Formula for calculating weight from object’s mass and acceleration
\(w = mg\)
where \(w`\) is the weight of the object in Newtons, \(m`\) is the mass of the object in kg and \(a`\) is the acceleration due to gravity on the planet. By default \(a = 9.80665\) m/s/s.
name:
weight
parameters:
object_mass
(float
expected - kg),planet_acceleration
(default=9.80665
,``float`` expected - m/s/s)return: object’s weight (float)
In your intergalactic.py
file you should write a program that asks the user for an objects mass, planet’s mass and planet’s radius. Your program should then tell the user the weight of the given object on Earth and the weight on the specified planet to 4 decimal places.
Example 1 (running from intergalactic.py
)
Object's mass (kg): 100
Other planet's mass (kg): 0.642e24
Other planet's radius (m): 3396000
Weight on Earth (Newtons): 980.6650
Weight on other Planet (Newtons): 371.5398
Example 2 (from intergalactic.py
)
Object's mass (kg): 50
Other planet's mass (kg): 1.898e27
Other planet's radius (m): 7.1492e7
Weight on Earth (Newtons): 490.3325
Weight on other Planet (Newtons): 1239.2446
Hint
We can represent very small numbers or very large numbers using scientific notation. eX
is used to represented \(\times 10^{X}\). For example 1e-3
is equivalent to \(1 \times 10^{-3}\) and 5e2
is equivalent to \(5\times10^{2}\). Python can automatically convert string representations of these values to floats.
Floats can convert strings with scientific notation.
x = '1e-3'
print(float(x))
y = '5e2'
print(float(y))
0.001
500.0
Solution
Solution is locked