03 Team Activity: Writing Functions
Instructions
Each lesson in CSE 111 contains a team activity that is designed to take about one hour to complete. You should prepare for all team activities by completing the preparation material and the individual checkpoint assignment before starting a team activity. The goal of the team activities is for students to work together and teach and learn from each other. As your team completes a team activity, instead of moving through it as quickly as you can, you should help everyone understand the concepts.
Face-to-Face Students
Face-to-face students will complete the team activities in their classroom during class time.
Online Students in a Semester Section
Online students in a semester section will arrange and participate in a one hour synchronous video meeting with your team for each team activity. As your team works through the assignment, be certain that each student can see the code that is being typed. When your team completes the video meeting, share the final Python file with everyone on your team.
Online Students in a Block Section
Students in a block section complete two lessons each week. This means that online students in a block section will complete two team activities each week. For one of these two activities, your team should arrange and participate in a one hour synchronous video meeting. For the other team activity, your team may meet in a synchronous video meeting or your team may collaborate, ask and answer questions, and share code in Microsoft Teams.
As your team works through a team assignment in a synchronous video meeting, be certain that each student can see the code that is being typed. When your team completes the video meeting, share the final Python file with everyone on your team.
Purpose
Boost your understanding of writing your own functions with parameters and then calling those functions with arguments.
Problem Statement
Health professionals who are helping a client achieve a healthy body weight, sometimes use two computed measures named body mass index and basal metabolic rate.
From the U.S. Centers for Disease Control and Prevention we read,
Body mass index (BMI) is a person’s weight in kilograms divided by the square of their height in meters. BMI can be used to screen for weight categories such as underweight, normal, overweight, and obese that may lead to health problems. However, BMI is not diagnostic of the body fatness or health of an individual.
The formula for computing BMI is
where weight is in kilograms and height is in centimeters.
Basal metabolic rate (BMR) is the minimum number of calories required daily to keep your body functioning at rest. BMR is different for women and men and changes with age. The revised Harris-Benedict formulas for computing BMR are
where weight is in kilograms and height is in centimeters.
Assignment
Work as a team to write a Python program named
fitness.py
that does the following:
- Asks the user to enter four values:
- gender
- birthdate in this format: YYYY-MM-DD
- weight in U.S. pounds
- height in U.S. inches
- Converts the weight from pounds to kilograms (1 lb = 0.45359237 kg).
- Converts inches to centimeters (1 in = 2.54 cm).
- Calculates age, BMI, and BMR.
- Prints age, weight in kg, height in cm, BMI, and BMR.
Helpful Documentation
- The preparation content for the previous lesson explains how to call functions.
- The prepare content for this lesson explains how to write functions.
- The official Python reference describes the built-in
round
function.
Steps
Copy and paste the following code into a new program named
fitness.py
. Use the pasted code as a design as you
write your program. Write code for each of the functions except
compute_age
. The compute_age
function is
complete and correct, and you should not change it.
# Import datetime so that it can be # used to compute a person's age. from datetime import datetime def main(): # Get the user's gender, birthdate, height, and weight. # Call the compute_age, kg_from_lb, cm_from_in, # body_mass_index, and basal_metabolic_rate functions # as needed. # Print the results for the user to see. pass def compute_age(birth_str): """Compute and return a person's age in years. Parameter birth_str: a person's birthdate stored as a string in this format: YYYY-MM-DD Return: a person's age in years. """ # Convert a person's birthdate from a string # to a date object. birthdate = datetime.strptime(birth_str, "%Y-%m-%d") today = datetime.now() # Compute the difference between today and the # person's birthdate in years. years = today.year - birthdate.year # If necessary, subtract one from the difference. if birthdate.month > today.month or \ (birthdate.month == today.month and \ birthdate.day > today.day): years -= 1 return years def kg_from_lb(pounds): """Convert a mass in pounds to kilograms. Parameter pounds: a mass in U.S. pounds. Return: the mass in kilograms. """ return def cm_from_in(inches): """Convert a length in inches to centimeters. Parameter inches: a length in inches. Return: the length in centimeters. """ return def body_mass_index(weight, height): """Compute and return a person's body mass index. Parameters weight: a person's weight in kilograms. height: a person's height in centimeters. Return: a person's body mass index. """ return def basal_metabolic_rate(gender, weight, height, age): """Compute and return a person's basal metabolic rate. Parameters weight: a person's weight in kilograms. height: a person's height in centimeters. age: a person's age in years. Return: a person's basal metabolic rate in kcals per day. """ return # Call the main function so that # this program will start executing.
Core Requirements
- Your program contains complete and correct functions named
compute_age
,kg_from_lb
, andcm_from_in
. - Your program contains complete and correct functions named
body_mass_index
andbasal_metabolic_rate
. To be correct, thebasal_metabolic_rate
function must compute BMR differently for males and females. - Your program contains a function named
main
which gets four values from the user, calls the other functions, and prints the results for the user to see.
Stretch Challenges
If your team finishes the core requirements in less than an hour, complete one or more of these stretch challenges. Note that the stretch challenges are optional.
- Modify your program to print the height values in meters instead of centimeters.
- Modify your program to allow the user to enter weight in British stones and add a function named
kg_from_stone
. - Modify your program to allow the user to enter height as U.S. feet and inches.
- Add something or change something in your program that you think would make your program better, easier for the user, more elegant, or more fun. Be creative.
Testing Procedure
Verify that your program works correctly by following each step in this testing procedure:
- Run your program and enter the inputs shown below. Ensure that your program’s output is similar* to the output below.
> python fitness.py Please enter your gender (M or F): F Enter your birthdate (YYYY-MM-DD): 2001-03-21 Enter your weight in U.S. pounds: 125 Enter your height in U.S. inches: 54 Age (years): 19 Weight (kg): 56.70 Height (cm): 137.2 Body mass index: 30.1 Basal metabolic rate (kcal/day): 1315 > python fitness.py Please enter your gender (M or F): M Enter your birthdate (YYYY-MM-DD): 2003-11-26 Enter your weight in U.S. pounds: 145 Enter your height in U.S. inches: 58 Age (years): 17 Weight (kg): 65.77 Height (cm): 147.3 Body mass index: 30.3 Basal metabolic rate (kcal/day): 1580
- Note that the output of your program will not be the same for the age and BMR values because a computer executed these sample runs before you executed your program. Therefore, when you run your program, the people will be older than they were when the computer completed these sample runs.
Sample Solution
Please work diligently with your team for the one hour meeting. After the meeting is over, please compare your solution to the sample solution. Please do not look at the sample solution until you have either finished the program or diligently worked for at least one hour. At the end of the hour, if you are still struggling to complete the assignment, you may use the sample solution to help you finish.
Call Graph
The following call graph shows the function calls and returns in the sample solution for this assignment. From this call graph we see the following function calls:
- The computer starts executing the sample program by calling the
main
function. - While executing the
main
function, the computer calls theinput
andfloat
functions. - While still executing the
main
function, the computer calls thecompute_age
,kg_from_lb
,cm_from_in
,body_mass_index
, andbasal_metabolic_rate
functions. - Finally, while still executing the main
function
, the computer calls theprint
function.
Ponder
After you finish this assignment, congratulate yourself because
you wrote a Python program with six user-defined functions named
main
, compute_age
,
kg_from_lb
, cm_from_in
,
body_mass_index
, and
basal_metabolic_rate
. You also wrote code to call those
six functions. Why is it important that you know how to write your
own functions?
Submission
When you have finished the activity, please report your progress via the associated I‑Learn quiz. When asked about which of the requirements you completed, feel free to include any work done during the team meeting or after the meeting, including work done with the help of the sample solution, if necessary. In short, report on what you were able to accomplish, regardless of when you completed it or if you needed help from the sample solution.