Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Let's walk through a complete example, from problem description, through problem solving to code and testing. Just read along and understand. Do not do

Let's walk through a complete example, from problem description, through problem solving to code and testing.
Just read along and understand. Do not "do" any of this until you are asked. Read it carefully and make sure you understand.
Problem Description:
Firstly, we understand that for some people, things like body image, weight and health can be sensitive issues.
There is no intention for the following scenario to be taken personally or thought of in any way that might cause concern.
It is a useful scenario for the purpose of breaking a problem into functions, which is why it is used.
According to cancer.org:
BMI is used to broadly define different weight groups in adults 20 years old or older. The same groups apply to both men and women.
BMI (Body Mass Index) can be calculated by the formula:
weight in kg /(height in m **2)
Remember "**2" means "to the power of 2"
The rough weight group categories defined by BMI are:
Underweight: BMI is less than 18.5
Normal weight: BMI is 18.5 to 24.9
Overweight: BMI is 25 to 29.9
Obese: BMI is 30 or more
This list of categories was taken from the cancer.org website, but we understand boundary conditions and should write our conditions in a better way. E.g., what category should a BMI of 29.95 be?
The client wants a program that will ask for a person's height and weight, then tell them their BMI and weight category.
Algorithm
When we look at the problem, we can break it up into a number of main sections. This is decomposition.
get inputs (making sure they're valid)
calculate BMI
determine weight category
Each of these can be implemented using functions.
Our main program algorithm will call the other functions.
We should notice that getting the weight and getting the height are very similar tasks that could be done with almost no difference, so this is a good situation for a reusable function.
Like we did in the lecture with get_valid_age, we'll pass in parameters to define the low and high bounds of the input.
Unlike that function, we'll also pass in the prompt so we can customise the function's print/output.
function main()
height = get_valid_number("Height",0,3)
weight = get_valid_number("Weight",0,300)
bmi = calculate_bmi(height, weight)
category = determine_weight_category(bmi)
print bmi, category
At this point, we have an algorithm that represents the whole program, but does not define any of the details. This is useful, but incomplete.
Let's define the algorithms for each of the functions now.
For the get_valid_number function, we should see that this uses the normal error-checking while loop pattern.
When a valid number has been entered, we "return" it.
function get_valid_number(prompt, low, high)
print prompt
get number
while number < low or number > high
print error, prompt
get number
return number
For the calculate_bmi function, we should see that this is a simple calculation/processing step.
When a result has been calculated, we return it.
function calculate_bmi(height, weight)
return weight /(height **2)
The job of the determine_weight_category function is not to print, but to return the category that has been determined by the function.
Upgraded pattern with functions
For a situation like this, we want a single, mutually exclusive result, so it seems fine to use the usual if-elif-else decision structure:
function determine_weight_category(bmi)
if bmi <18.5
return underweight
else if bmi <25
return normal
else if bmi <30
return overweight
else
return obese
However, since we are now using a function, the "else" is redundant. If any one of these conditions is True, the function returns, so we know it will only get to the next condition if the previous one was False.
If we run pylint to check the code for the above algorithm, we would get the message:
Unnecessary "else" after "return", remove the "else" and de-indent the code inside it (no-else-return)
So, let's "upgrade" this pattern, noting that this ONLY makes sense for use within a function because of how return works:
function determine_weight_category(bmi)
if bmi <18.5
return underweight
if bmi <25
return normal
if bmi <30
return overweight
return obese
We could "desk check" each of these pseudocode functions.
E.g., if we put a BMI value of 27 into the determine_weight_category function, we should get "overweight".
Code
When it comes to coding functions, you can either write main first and then each function (top-down), or write each function and then put it all together in main (bottom-up).
There's no right answer, but we're going to do it bottom-up and test our functions one at a time before putting them all together.
Look at how very similar the Python code is to the algorithm for the first function we'll write. This would be ne

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image_2

Step: 3

blur-text-image_3

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Oracle9i Database Administrator Implementation And Administration

Authors: Carol McCullough-Dieter

1st Edition

0619159006, 978-0619159009

More Books

Students also viewed these Databases questions

Question

How can gifts be used to lower the overall tax paid by a family?

Answered: 1 week ago