🚚

Project 2 - Shipping Cost Calculator Overview

Aug 25, 2025

Overview

This lecture demonstrates the step-by-step development of a shipping cost calculator program, illustrating key concepts such as input validation, conditionals, constants, formatted output, and looping for multiple packages.

Problem Specification & Requirements

  • The program calculates shipping costs for a logistics company based on package dimensions.
  • User inputs length, width, and height in inches (each must be 10 inches or less).
  • If any dimension exceeds 10 inches, the package is unshippable.
  • Base shipping cost is $2.50.
  • Volume is calculated as length × width × height.
  • Surcharges:
    • 10% if volume > 200 cubic inches.
    • 25% if volume > 500 cubic inches (only the highest applies).
  • Use formatted strings (f-strings) for output.

Program Design & Key Concepts

  • Avoid "magic numbers" by defining constants for all fixed values (e.g., MAX_DIMENSION, BASE_COST, SURCHARGE_TIERS).
  • Gather user input and ensure values are numeric (preferably using float() or int()).
  • Input validation uses an if-elif-else structure to check dimensional constraints.
  • Program prints appropriate messages for valid or unshippable packages.
  • Output is formatted to two decimal places for dollar amounts.
  • Optional: handle zero or negative dimensions as invalid.

Step-by-Step Coding Process

  • Greet the user and prompt for package dimensions.
  • Validate dimensions; reject and print a message if any exceed 10 inches.
  • If valid, calculate package volume and shipping cost:
    • Apply surcharges based on defined volume tiers.
    • Display formatted results including volume and cost.
  • Add final thank-you message, always displayed at the end.

Extending the Program

  • To handle multiple packages, wrap single-package logic in a for-loop.
  • Prompt user for the number of packages to process.
  • For each package, display package number and collect/process input as above.
  • Optional enhancements include:
    • Skipping packages with zero or negative dimensions.
    • Calculating and displaying the total shipping cost for all packages.
    • Adding additional surcharge tiers as needed.

Key Terms & Definitions

  • Constant — a variable set once and not changed, used to store fixed values (e.g., MAX_DIMENSION = 10).
  • Magic Number — a hardcoded literal value without explanation, which should be replaced by a named constant.
  • Surcharge — an extra cost added to the base shipping price depending on package volume.
  • f-string — a Python string formatting method allowing expressions inside curly brackets, e.g., f"Cost: ${cost:.2f}".

Action Items / Next Steps

  • Practice writing a shipping calculator program following these guidelines.
  • Experiment by adding input validation for illegal or negative values.
  • Extend the program to calculate the total cost for all packages shipped.
  • Review constants and logic for easy updates, and add more surcharge tiers if desired.