Programming Exercise 3-1
Programming Exercise 5-6
# Global constants for calories
CALORIES_FROM_FAT = 9
CALORIES_FROM_CARBS = 4
# main module
def main():
# Local variables
gramsFat = 0.0
gramsCarbs = 0.0
caloriesFat = 0.0
caloriesCarbs = 0.0
# Get grams fat.
gramsFat = float(input(‘Enter the fat grams consumed: ‘))
# Get grams carbs.
gramsCarbs = float(input(‘Enter the carbohydrate grams consumed: ‘))
# Calculate calories from fat.
caloriesFat = gramsFat * CALORIES_FROM_FAT
# Calculate calories from carbs.
caloriesCarbs = gramsCarbs * CALORIES_FROM_CARBS
# Print calories.
showCarbs(gramsFat, gramsCarbs, caloriesFat, caloriesCarbs)
# The showCarbs function accepts the number of grams of fat and
# of carbs, as well as the calories from fat and from carbs, as
# arguments and displays the resulting calories.
def showCarbs(gramsFat, gramsCarbs, caloriesFat, caloriesCarbs):
print(‘Grams of fat: ‘, format(gramsFat, ‘.2f’))
print(‘Fat calories: ‘, format(caloriesFat, ‘.2f’))
print(‘Grams of carbs: ‘, format(gramsCarbs, ‘.2f’))
print(‘Carb calories: ‘, format(caloriesCarbs, ‘.2f’))
# Call the main function.
main()
_1254916956.vsd
main()
getFat
(gramsFat)
setFat
(gramsFat, caloriesFat)
End
Declare
gramsFat, gramsCarbs,
caloriesFat, caloriesCarbs
getFat
(Ref inputFat)
Return
Display “Enter
fat grams
consumed”
Input
inputFat
showCarbs
I(gramsFat, gramsCarbs,
caloriesFat, caloriesCarbs)
Return
Display
“Grams of Fat: “, gramsFat
“Result calories: “, caloriesFat
“Grams of Carbs: “, gramsCarbs
“Result calories: “, caloriesCarbs
Global Constant Real
CALORIES_FROM_FAT
CALORIES_FROM_CARBS
getCarbs
(gramsCarbs)
getCarbsl
(Ref inputCarbs)
showCarbs
(gramsFat,
gramsCarbs,
caloriesFat,
caloriesCarbs)
setFat
(gramsFat, caloriesFat)
Return
caloriesFat = gramsFat *
CALORIES_FROM_FAT
Display “Enter
carbohydrate
grams consumed”
Return
Input
inputCarbs
setCarbs
(gramsCarbs,
caloriesCarbs)
setCarbs
(gramsCarbs,
caloriesCarbs)
Return
caloriesCarbs = gramsCarbs *
CALORIES_FROM_CARBS
main()
getFat
(gramsFat)
setFat
(gramsFat, caloriesFat)
End
Declare
gramsFat, gramsCarbs, caloriesFat, caloriesCarbs
getFat
(Ref inputFat)
Input
inputFat
showCarbs
I(gramsFat, gramsCarbs, caloriesFat, caloriesCarbs)
Return
Return
Display “Enter fat grams consumed”
Display
“Grams of Fat: “, gramsFat
“Result calories: “, caloriesFat
“Grams of Carbs: “, gramsCarbs
“Result calories: “, caloriesCarbs
Global Constant Real CALORIES_FROM_FAT
CALORIES_FROM_CARBS
setCarbs
(gramsCarbs, caloriesCarbs)
getCarbs
(gramsCarbs)
getCarbsl
(Ref inputCarbs)
Return
Input
inputCarbs
Display “Enter carbohydrate grams consumed”
showCarbs
(gramsFat, gramsCarbs, caloriesFat, caloriesCarbs)
setFat
(gramsFat, caloriesFat)
Return
caloriesFat = gramsFat * CALORIES_FROM_FAT
setCarbs
(gramsCarbs, caloriesCarbs)
Return
caloriesCarbs = gramsCarbs * CALORIES_FROM_CARBS