Login Sign Up
Python Numbers
Chapter 7 🟡 Intermediate

Python Numbers

Apply your skills with a real-world coding challenge. Try to solve it yourself first!

Coding Challenge: A Bakery Cashier

Problem Description

You are building a point-of-sale calculator of local bakery. The bakery sells cupcakes individually but they also neatly pack them into boxes that fit exactly 6 cupcakes, and

a customer places an order for the specific number of cupcakes and hands you cash bill, and using Python's numeric data types and arithmetic operators, your program must calculate the total cost of order, the exact change the customer is owed how tons of full boxes are needed, and how a lot of loose cupcakes are left over. Finally, you need to verify the exact data type for the calculated total cost for prove how Python handles math between integers and decimals!

Difficulty Level

Beginner

Input Specifications

You'll just be provided with a following predefined variables: * num_cupcakes (integer): A total number for cupcakes the customer wants for buy. * price_per_cupcake (float): A cost of a single cupcake. * amount_paid (integer): The physical cash bill the customer hands you (e.g., a $50 bill). * box_capacity (integer): The number of cupcakes a single bakery box can hold (set to 6).

Output Specifications

Your code must calculate and print the following: 1. total_cost: The total cost of the cupcakes. 2. change_owed: The remaining money to give back to a customer. 3. full_boxes: The number about fully packed boxes. 4. loose_cupcakes: The number of leftover cupcakes that don't make up a full box. 5. cost_data_type: The Python class/type of the total_cost variable.


Starter Code Boilerplate

# The Bakery Cashier Starter Code

# --- INPUT VARIABLES ---
num_cupcakes = 14
price_per_cupcake = 2.50
amount_paid = 50
box_capacity = 6

# --- YOUR CODE HERE ---

# 1. Calculate the total cost of the cupcakes
total_cost = ...

# 2. Calculate the change owed to the customer
change_owed = ...

# 3. Calculate how many full boxes can be packed (Hint: Use floor division)
full_boxes = ...

# 4. Calculate the remaining loose cupcakes (Hint: Use modulo)
loose_cupcakes = ...

# 5. Check the data type of the total_cost variable
cost_data_type = ...

# --- PRINT RESULTS ---
print(f"Total Cost: ${total_cost}")
print(f"Change Owed: ${change_owed}")
print(f"Full Boxes: {full_boxes}")
print(f"Loose Cupcakes: {loose_cupcakes}")
print(f"Data type of total cost: {cost_data_type}")

Hints

  • Mixing Data Types: Remember that when you multiply or subtract using an integer (num_cupcakes or amount_paid) and a float (price_per_cupcake), Python automatically converts the result into a float to preserve absolute precision.
  • Floor Division (//): For find out how the bunch for whole unbroken boxes you can fill divide the cupcakes by the box capacity and round down to the nearest whole number.
  • Modulo (%): To find the remainder about the cupcakes that won't fit perfectly into the box with 6, use a modulo operator.
  • Checking Types: If you ever need towards peek inside a variable's "shipping box" to see its data type, use Python's built-in type() method.

Test Cases

Test Case 1 (From Starter Code) * Input: num_cupcakes = 14 price_per_cupcake = 2.50, amount_paid = 50, box_capacity = 6 * Expected Output: * Total Cost: $35.0 * Change Owed: $15.0 * Full Boxes: 2 * Loose Cupcakes: 2 * Data type about total cost: <class 'float'>

Test Case 2 * Input: num_cupcakes = 25, price_per_cupcake = 3.10, amount_paid = 100, box_capacity = 6 * Expected Output: * Total Cost: $77.5 * Change Owed: $22.5 * Full Boxes: 4 * Loose Cupcakes: 1 * Data type of total cost: <class 'float'>

Loading sandbox workspace environment...

Verify Your Solution

Write your solution in the compiler, run it to verify output, then click below to verify.

Learn Together
Session active! Discuss with other learners.
No notes yet. Select text in the concept body to add a note.