Create a project folder username_a05
. Place all your tasks python files(t01.py
,
t02.py
,t03.py
) and the testing file testing.txt
inside this folder. Upon
completing your tasks, export your projrct folder to username_a05.zip
file, validate it, and
submit it to the dropbox by the due date.
Write and test the following function:
def factorial(num):
"""
-------------------------------------------------------
Calculates and returns the factorial of num.
Use: product = factorial(num)
-------------------------------------------------------
Parameters:
num - number to factorial (int > 0)
Returns:
product - num! (int)
------------------------------------------------------
"""
Add the function to a Python module named functions.py
.
Test it from .
In mathematics, the notation n!
represents the factorial
of the non-negative integer n
. The factorial of n
is the product of all the non-negative integers from 1 to n
.
Examples:
7! → 1 × 2 × 3 × 4 × 5 × 6 × 7 → 5040
4! → 1 × 2
× 3 × 4 → 24
The function must use a for
loop.
The function does not ask for input and does no printing - that is done by your test program.
Sample execution:
factorial(7)
5040
factorial(1)
1
Test the function with three different sets of parameters.
Partial testing:
Write and test the following function:
def calories_burned(per_minute, minutes):
Add the function to a Python module named functions.py
.
Test it from .
Running on a treadmill burns a certain number of calories. calories_burned
prints a table of the number of calories burned every five minutes
given the number of calories burned per minute (per_minute
)
an the total number of minutes run (minutes
). Align the
results and print with 1 decimal acurracy for the calories burned as
shown in the example execution.
Provide the function docstring (documentation) following the CP104 style.
The function must use a for
loop.
The function does not ask for input.
Sample execution:
calories_burned(4.1, 30)
5: 20.5 10: 41.0 15: 61.5 20: 82.0 25: 102.5 30: 123.0
Test the function with three different sets of parameters.
Partial testing:
Write and test the following function:
def range_total(start, increment, count):
"""
-------------------------------------------------------
Uses a for loop to sum count values from start by increment.
Use: total = range_total(start, increment, count)
-------------------------------------------------------
Parameters:
start - the range start value (int)
increment - the range increment (int)
count - the number of values in the range (int)
Returns:
total - the sum of the range (int)
------------------------------------------------------
"""
Add the function to a Python module named functions.py
.
Test it from .
The function must use a for
loop.
Example: the sum of 5 values starting from 2 with an increment of 2
is:
2 + 4 + 6 + 8 + 10 → 30
The function does not ask for input and does no printing - that is done by your test program.
Sample execution:
range_total(1, 1, 5)
15
range_total(2, 2, 5)
30
Test the function with three different sets of parameters.
Partial testing:
Partial testing of functions.py:
Partial testing of testing.txt: