You are to implement (code in Python) the calorie intake assistant as outlined in the planning document and noting the changes required below. Use what you have learned in class,including functions, selection, repetition and lists. You should use any feedback that you received from Assignment 1 to improve your program flow.
Answer:
Functions List
- getValue(): This function takes an input from the user for the mentioned prompt passed as the parameter and return it back.
- checkValid(): This function compares the value of the first parameter with all the other parameters passed if it matches any of them.
- checkValidBetween(): This function compares the value of the first parameter with the other two parameters if it lies in between of the two.
- calculate(): This function takes up the parameters which are required to calculate the RDCI and then return the RDCI back to the calling function.
- checkPercent(): This function calculates the percentage of RDCI as calorie intake on the basis of healthy/unhealthy food taken by the user.
- calculateTemptation(): This function generates a random temptation and the calorie intake for the user and informs the user about the same if there is any tempatation.
- displayResult(): This function display the message to the user on the basis of the average RDCI and the percentage and the calorie intake by the user in the week.
- main(): This function defines the flow of the program and is responsible for the desired output.
function getValue():
IPO
Input
|
Processing
|
Output
|
ques
|
Display ques
Get ans from user
|
ans
|
Algorithm
getValue(ques):
display ques
get ans from user
return ans
function checkValid():
IPO
Input
|
Processing
|
Output
|
val
valid1
valid2
|
Compare val with valid1 and valid2
|
true
false
|
Algorithm
checkValid(val, valid1, valid2):
if(val is equal to valid1 or valid2)
return true
return false
Condition Action Table
Condition
|
Action
|
val is equal to valid1
|
true is returned
|
val is equal to valid2
|
true is returned
|
val is not equal to either valid1 or valid2
|
false is returned
|
function checkValidBetween():
IPO
Input
|
Processing
|
Output
|
val
lower
higher
|
val is compared with lower and higher
|
true
false
|
Algorithm
checkValidBetween(val, lower, higher):
if(val is between lower and higher)
return true
return false
Condition Action Table
Condition
|
Action
|
val is between lower and higher
|
true is returned
|
val is not between lower or higher
|
false is returned
|
function calculate():
IPO
Input
|
Processing
|
Output
|
gender
age
weight
height
|
-
|
RDCI
|
Algorithm
if(gender is 'F'):
RDCI = (10 * weight) + (6.25 * height) - (5 * age) - 161
otherwise
RDCI = (10 * weight) + (6.25 * height) - (5 * age) + 5
return RDCI
Condition Action table
Condition
|
Action
|
gender is ‘F’
|
Calculate and return RDCI
|
gender is ‘M’
|
Calculate and return RDCI
|
function checkPercent():
IPO
Input
|
Processing
|
Output
|
val
|
-
|
percentage
|
Algorithm
if(val is 1)
return 1.5
otherwise if(val is 2)
return 1.2
otherwise if(val is 3)
return 1
otherwise if(val is 4)
return 0.8
Condition Action Table
Condition
|
Action
|
val is 1
|
1.5 is returned
|
val is 2
|
1.2 is returned
|
val is 3
|
1 is returned
|
val is 4
|
0.8 is returned
|
function checkTemptation():
IPO
Input
|
Processing
|
Output
|
-
|
Calculate temptation
|
val
|
Algorithm
val = random no 0 or 1
if(val is 0)
return 0
otherwise
display " It also looks like this day you’ve been tempted! Naughty..."
val = random no between 50 and 150
return val
Condition Action Table
Condition
|
Action
|
val is 0
|
0 is returned
|
val is 1
|
random no between 50 and 150
|
function displayResult():
IPO
Input
|
Processing
|
Output
|
name, RDCI, total
|
Calculate avg percent
|
-
|
Algorithm
display "Thank you! I’m computing your results…"
ADCI = total/7
val = ADCI/RDCI * 100
if(val is less than 90)
display name + " your daily calorie intake is lower than the recommended with" + (val - 100) + "%. This rhythm will make you lose weight,
just make sure your meals contain all nutritional value needed. It’s recommended that you do not fall under the healthy weight and that you keep a
balanced lifestyle."
otherwise if(val is between 90 and 110)
display name + "your daily calorie intake is close to the recommended one! You have a balanced healthy lifestyle, well done!"
otherwise
display name + "your daily calorie intake is higher than the recommended with " + (val -100) + "%. This rhythm will make you gain weight
which will lead to health concerns. It’s recommended that you either lower your calorie intake, either exercise more! Careful with those
temptations!"
Condition Action Table
Condition
|
Action
|
val is less than 90
|
Show a confirmation hard coded message
|
val is between 90 and 110
|
Show a confirmation hard coded message
|
val is above 110
|
Show a confirmation hard coded message
|
function main():
IPO
Input
|
Processing
|
Output
|
-
|
Input all the values from the user and calculates the calorie intake for the user
|
-
|
Algorithm
name = getValue("Welcome to the Calorie Intake Assistant, my name is Cal. What is your name?")
while(length of name is 0)
name = getValue("Don’t be shy! What is your name?")
val = getValue("Hi" + name + "! To properly assist you, we will need some personal information such as age, gender, weight and height. Do you
want to continue? Enter Y for yes or N for no.")
valCond = (checkValid(check, 'Y', 'N')
while(valCond is false)
val = getValue("I’m sorry, I cannot understand. To properly assist you, we will need some personal information such as age, gender, weight
and height. Do you want to continue? Enter Y for yes or N for no.")
valCond = (checkValid(check, 'Y', 'N')
if(val is 'N')
display "That’s alright, good bye " + name
Exit
gender = getValue("Great " + name + ", let’s start! What is your gender? Enter M for male or F for female.")
genderCond = checkValid(gender, 'M', 'F')
while(genderCond is false)
gender = getValue(" I’m sorry, I cannot understand. What is your gender? Enter M for male or F for female.")
genderCond = checkValid(gender, 'M', 'F')
age = getValue("What is your age in years?")
ageCond = checkValidBetween(age, 18, 99)
while(ageCond is false)
age = getValue("I’m sorry, I cannot understand. What is your age in years?")
ageCond = checkValidBetween(age, 18, 99)
weight = getValue("What is your current weight in kg?")
weightCond = checkValidBetween(weight, 40, 150)
while(weightCond is false)
weight = getValue("I’m sorry, I cannot understand. What is your current weight in kg?")
weightCond = checkValidBetween(weight, 40, 150)
height = getValue("What is your height in cm?")
heightCond = checkValidBetween(height, 80, 230)
while(heightCond is false)
height = getValue("I’m sorry, I cannot understand. What is your height in cm?")
heightCond = checkValidBetween(height, 80, 230)
display "Thank you for the information " + name + "! Let’s see how healthy your meals were last week"
RDCI = calculate(gender, age, weight, height)
intialise calorie to 0
num = 1
while(num is less than or equal to 7)
val = getValue("Day " + num + ": were your meals very unhealthy (1), unhealthy (2), healthy (3), or very healthy (4)? Enter the
corresponding number.")
intake = RDCI * checkPercent(val)
temp = calculateTemptation()
add intake and temp to calorie
displayResult(name, RDCI, calorie)
display "Goodbye and good luck!"
Condition Action Table
Condition
|
Action
|
val is ‘N’
|
say goodbye and exit
|
val is ‘Y’
|
Execute the program in the desired flow
|
Loop Tables
Loop variable
|
Exit conditions
|
valCond
|
valCond is true
|
genderCond
|
genderCond is true
|
ageCond
|
ageCondd is true
|
weightCond
|
weightCond is true
|
heightCond
|
heightCond is true
|
num
|
num is more than 7
|