87.2k views
5 votes
For this lab you will do 2 things:

Solve the problem in an algorithm

Code it in Python

Problem:


Cookie Calories


A bag of cookies holds 40 cookies. The calorie information on the bag claims that there are 10 "servings" in the bag and that a serving equals 300 calories. Write a program that asks the user to input how many cookies he or she ate and the reports how many total calories were consumed.


*You MUST use constants to represent the number of cookies in the bag, number of servings, and number of calories per serving. Remember to put constants in all caps!


Make sure to declare and initialize all variables before using them!


Then you can do the math using those constants to find the number of calories in each cookie.


Make this program your own by personalizing the introduction and output.




Sample Output:


WELCOME TO THE CALORIE COUNTER!!

1 Answer

1 vote

Answer:

Here is the Python program:

COOKIES_PER_BAG = 40 #sets constant value for bag of cookies

SERVINGS_PER_BAG = 10 #sets constant value for serving in bag

CALORIES_PER_SERVING = 300 #sets constant value servings per bag

cookies = int(input("How many cookies did you eat? ")) #prompts user to input how many cookies he or she ate

totalCalories = cookies * (CALORIES_PER_SERVING / (COOKIES_PER_BAG / SERVINGS_PER_BAG)); #computes total calories consumed by user

print("Total calories consumed:",totalCalories) #displays the computed value of totalCalories consumed

Step-by-step explanation:

The algorithm is:

  • Start
  • Declare constants COOKIES_PER_BAG, SERVINGS_PER_BAG and CALORIES_PER_SERVING
  • Set COOKIES_PER_BAG to 40
  • Set SERVINGS_PER_BAG to 10
  • Set CALORIES_PER_SERVING to 300
  • Input cookies
  • Calculate totalCalories: totalCalories ← cookies * (CALORIES_PER_SERVING / (COOKIES_PER_BAG / SERVINGS_PER_BAG))
  • Display totalCalories

I will explain the program with an example:

Lets say user enters 5 as cookies he or she ate so

cookies = 5

Now total calories are computed as:

totalCalories = cookies * (CALORIES_PER_SERVING / (COOKIES_PER_BAG / SERVINGS_PER_BAG));

This becomes:

totalCalories = 5 * (300/40/10)

totalCalories = 5 * (300/4)

totalCalories = 5 * 75

totalCalories = 375

The screenshot of program along with its output is attached.

For this lab you will do 2 things: Solve the problem in an algorithm Code it in Python-example-1
User Alexmac
by
5.7k points