1️⃣ Word Problem Solving Method
What is a Word Problem?
A word problem presents a real-world situation using everyday language. The objective is to translate this scenario into Python code that can analyze data and solve the problem.
In data science, code written to solve a problem does not simply calculate an answer for specific numbers in that problem. Instead, the solution must handle different data — the data you have today, new data you collect tomorrow, or any similar data in the future.
Variables should contain the raw data from the problem. Python should perform all calculations.
Pre-calculating values manually defeats the purpose of creating adaptable code. When Python performs the computations, the code can adapt as data changes.
📋 The Five-Steps Method
A systematic approach to word problem solving consists of five steps:
1. READ & UNDERSTAND
The problem should be read carefully to identify:
- Given information: What data is available?
- Goal: What must be found or computed?
- Constraints: Are there any conditions or limitations?
2. EXTRACT DATA & UNITS
All numerical values and their units should be listed. Creating a table is often helpful:
- Each number should be recorded with its unit (dollars, meters, seconds, etc.)
- Any necessary conversion factors should be noted
- What each number represents should be identified
3. NAME VARIABLES & ASSIGN RAW DATA
Critical concept: Variables should store the original data from Step 2, not calculated results.
Descriptive variable names should include units:
- Good:
price_in_dollars,distance_km,time_seconds - Poor:
x,p,num
Key principle: Variables represent the changeable parts of the problem. Storing original data allows Python to recalculate when values change.
4. PLAN THE CALCULATION
Before coding, the formula or algorithm should be written in plain language:
- What mathematical operations are needed?
- In what order should calculations occur?
- Will intermediate results be needed?
5. IMPLEMENT & VERIFY
The code should be written using the plan from Step 4:
- Intermediate results should be stored in named variables
- Comments should explain the logic
- The code should be tested with example data
- The answer should be checked for reasonableness
💡 Worked Example: The Pizza Party Problem
Problem statement:
A school class of 24 students is ordering pizza for a party. Each large pizza costs $12.50 and has 8 slices. Each student will eat an average of 3 slices. There is an 8% sales tax, and the class wants to add a 15% tip on the pre-tax amount. Calculate the total cost per student.
Solution Using The Five-Step Method:
1. READ & UNDERSTAND
Given: 24 students, 3 slices per student, 8 slices per pizza, $12.50 per pizza, 8% tax, 15% tip
Goal: Determine the total cost per student
Constraints: Must account for tax and tip correctly
2. EXTRACT DATA & UNITS
| Quantity | Value | Unit |
|---|---|---|
| Number of students | 24 | |
| Slices per student | 3 | |
| Slices per pizza | 8 | |
| Price per pizza | 12.50 | dollars |
| Sales tax rate | 0.08 | |
| Tip rate | 0.15 |
3. NAME VARIABLES
number_of_students = 24 slices_per_student = 3 slices_per_pizza = 8 price_per_pizza_dollars = 12.50 sales_tax_rate = 0.08 tip_rate = 0.15
4. PLAN THE CALCULATION
Algorithm:
- Calculate total slices needed: students × slices per student
- Calculate pizzas needed: total slices ÷ slices per pizza (round up!)
- Calculate subtotal: pizzas × price per pizza
- Calculate tip: subtotal × tip rate
- Calculate tax: subtotal × tax rate
- Calculate total: subtotal + tip + tax
- Calculate cost per student: total ÷ number of students
5. IMPLEMENT & VERIFY
# Step 1: Define all given values
number_of_students = 24
slices_per_student = 3
slices_per_pizza = 8
price_per_pizza_dollars = 12.50
sales_tax_rate = 0.08
tip_rate = 0.15
# Step 2: Calculate total slices needed
total_slices_needed = number_of_students * slices_per_student
# Step 3: Calculate number of pizzas (round up)
pizzas_needed = (total_slices_needed // slices_per_pizza + 1)
# Step 4: Calculate subtotal before tax and tip
subtotal_dollars = pizzas_needed * price_per_pizza_dollars
# Step 5: Calculate tip (on pre-tax amount)
tip_dollars = subtotal_dollars * tip_rate
# Step 6: Calculate tax (on pre-tax amount)
tax_dollars = subtotal_dollars * sales_tax_rate
# Step 7: Calculate total cost
total_cost_dollars = subtotal_dollars + tip_dollars + tax_dollars
# Step 8: Calculate cost per student
cost_per_student_dollars = total_cost_dollars / number_of_students
# Display results with clear labels
print("=== PIZZA PARTY COST BREAKDOWN ===")
print("Total slices needed: " + str(total_slices_needed) + " slices")
print("Pizzas to order: " + str(pizzas_needed) + " pizzas")
print("Subtotal: " + str(subtotal_dollars))
print("Tip (15%):" + str(tip_dollars))
print("Tax (8%): " + str(tax_dollars))
print("Total cost: " + str(total_cost_dollars))
print("Cost per student: " + str(cost_per_student_dollars))
Verification Check: Does this answer make sense?
- 9 pizzas for 24 students eating 3 slices each seems reasonable ✓
- $5.77 per student for pizza with tax and tip is realistic ✓
- The tip and tax percentages were applied correctly ✓
⚠️ Common Errors to Avoid
Error 1: Non-Descriptive Variable Names
Poor practice:
x = 24 y = 3 z = x * y # What does this calculate?
Better practice:
number_of_students = 24 slices_per_student = 3 total_slices = number_of_students * slices_per_student # Clear!
Error 2: Omitting Units
Units should always be included in variable names, and unit consistency should be verified. When mixing units (e.g., meters and kilometers), conversion must be performed first.
Error 3: Failing to verify results
The reasonableness of answers should always be checked in real-world context. If calculating cost per person yields $10,000, an error has occurred.
Remember: The key to solving word problems is systematic translation from natural language to code. Take your time with each step, and always verify your work!
