COMP 118 – Fun with candy bars

Complete this program. If the program is completed in the repl project, replace the code below.

  1. We will be using the online programming education tool codeboard.io
    • Open the following link in a new browser tab (Middle Click): Link
    • Edit the project and submit when done.
  2. You now have your own copy of the code. Copy and save the new URL.
  3. Play around with the program by running it a few times.
  4. The comments in the code give instructions on features to add.
  5. Add the features, and turn in the link to your repl.it code on moodle.
  6. This lab is just to give you a chance to begin to learn how to program. I do not expect it to be perfect, or even a particular solution. There are several things to think about, such as how to format the number output, and how to deal with bites (a float or integer?). You should come up with what you think is best. You grade will only depend on your completing all the steps of the lab, not the specifics on how you decide to solve the problem.
  7. floor() and ceil() functions. You might find this useful. Floor takes a float and removes the decimal part, e.g. math.floor(3.25) becomes 3. Ceil find the ceiling, or the next higer integer. math.ceil(3.25) becomes 4. You must include “import math” at the start of your program for these to work. Here is some sample code: https://codeboard.io/projects/454319

Turn the URL for your rel into moodle by the due time.

The start code:

# Computations with Candy Bars
print("This program will allow you to make calculations about your bag of candy bars.")
nameCandyBar = input("What kind of candy bars do you have? ")
print("Answer the following questions about your",nameCandyBar,"bars.")
numCandyBar = int(input("Enter the number of candy bars in a package: "))
weightCandyBar = float(input("Enter the weight in ounces of one candy bar: "))
totalWeight = weightCandyBar * numCandyBar

print(numCandyBar,nameCandyBar,"bars")
print(weightCandyBar," ounces each")
print("Total weight is ",totalWeight,"ounces.")

print("Perhaps you should consider a healthier snack!")

# Now add a new variable caloriesPerBar. 
# What should be the type?
# Add an input prompt asking the user to 
# enter the number of calories per
# bar. 

# Add a statement to compute and output the 
# total number of calories given the number 
# of candy bars.



#  Assume that the typical bite of a candy bar 
# is 0.7 ounces. Add code to display the number 
# of bites per bar, and the total number of bites 
# for all the bars. What types did you use for the variables? Why?
# For example if a bar is 2.1oz, then the number of bites is
# 2.1/0.7 which is 3 bites.


# A person burns (weight * .57) calories per 
# mile walking, and (weight * .72) calories 
# per mile running. Ask the person for 
# their weight, and tell them how many miles 
# they would need to run or walk to burn off 
# one candy bar, and how many for the whole
# package.


# Write out  message, as a sentence, explaining
# how much exercise must be done to use up 
# the calories of the candy bars, then encourage 
# the user to eat something healthier.  
# Include the name of the candy bars in 
# the output.
Code language: PHP (php)

Grading Table

RequirementGrading CommentsPointsScore
Completed all components in the lab60
Program works correctly40
Total100
Scroll to Top