
Goal: Try out online programming, explore how code runs, and have fun!
What You’ll Use
We’ll use an online programming environment called OnlineGDB — no installation needed!
Instructions
1. Open the Starter Project
- Open the provided OnlineGDB link in a new browser tab (CandyBars)
- This will create your own copy of the code.
Important: Copy and save the URL from your browser — this is your version! - If you are not logged in, log in with your Kenyon email using the google option.
2. Explore the Program
- Click Run to execute the program a few times.
- Look at how it works and what it prints.
- Read the comments in the code — they guide you through edits to make.
3. Edit and Enhance
- Modify the program by following the instructions in the comments.
- Don’t forget to hit the “Save” button to save you work. Give it a meaningful name.
- You might need to:
- Adjust formatting of numbers
- Decide whether to use floats or integers for “bites”
- Use mathematical functions like
floor()orceil()
Tip:
You’ll need to addimport mathat the top of your program to use:
pythonCopyEditmath.floor(3.25) # → 3
math.ceil(3.25) # → 4
4. Submit Your Work
You must submit your work in two ways:
- On OnlineGDB:
- Use the “Save” button inside OnlineGDB
- On Moodle:
- Paste the URL of your saved program into the assignment submission field before the due date
Purpose of the Lab
This lab is designed to help you:
- Get comfortable with editing and running code
- Think about data types, formatting, and logic
- Practice submitting and sharing your work
You don’t need to be perfect!
As long as you complete all steps, try out the features, and produce reasonable output, you’ll earn full credit.
The start code:
# 🍫 Computations with Candy Bars
# Computations with Candy Bars
import math # Needed for floor/ceil if used later
print("This program will help you calculate details about your bag of candy bars.\n")
# --- Step 1: Basic info about the candy bars ---
candy_name = input("What kind of candy bars do you have? ")
print(f"Answer the following questions about your {candy_name} bars.\n")
num_bars = int(input("Enter the number of candy bars in a package: "))
weight_per_bar = float(input("Enter the weight (in ounces) of one candy bar: "))
total_weight = num_bars * weight_per_bar
print("\nYou have",num_bars,candy_name,"bars.")
print("Each bar weighs",format(weight_per_bar,".2f"),"ounces.")
print(f"Total weight:",format(total_weight,".2f"),"ounces.")
print("\nPerhaps you should consider a healthier snack!\n")
# --- Step 2: Calories ---
# TODO: Add a variable for calories per bar.
# Ask the user for input.
# Compute total calories and display the result.
# --- Step 3: Bites per bar ---
# Assume 1 bite = 0.7 oz.
# TODO: Calculate and display:
# - Bites per bar
# - Total bites for all bars
# Example: if a bar is 2.1oz, then 2.1 / 0.7 = 3 bites.
# --- Step 4: Burn calories ---
# A person burns:
# - weight * 0.57 calories per mile walking
# - weight * 0.72 calories per mile running
# TODO: Ask user for their weight.
# Then calculate and display how many miles they need to:
# - Walk to burn off 1 bar and the whole package
# - Run to burn off 1 bar and the whole package
# Include candy bar name in the output.Code language: PHP (php)
Grading Table
| Requirement | Grading Comments | Points | Score |
|---|---|---|---|
| Completed all components in the lab | 60 | ||
| Program works correctly | 40 | ||
| Total | 100 |
