Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Learn Challenge: Material Cost Calculator | Architectural Data Analysis with Python
Python for Architects
Sectionย 1. Chapterย 5
single

single

bookChallenge: Material Cost Calculator

Swipe to show menu

In architectural projects, managing material costs is a crucial part of staying within budget. You often need to calculate not only the cost of each material but also the total cost for the entire project. Suppose you are given two dictionaries: one representing the quantities of materials required and another representing the unit cost for each material. Your task is to write a function that computes the total cost for each material and then sums these to provide the overall project cost, storing this under the key "Total" in the result.

To solve this, you will:

  • Iterate through the materials;
  • Multiply each material's quantity by its unit cost;
  • Store the result in a new dictionary;
  • After calculating all individual material costs, sum these values to get the total project cost and add it to the dictionary with the key "Total".

This approach ensures that you have both a detailed breakdown and a single summary value, which is useful for architects during project planning.

12345678910111213141516171819202122232425262728
material_quantities = { "Concrete": 100, "Steel": 50, "Glass": 30 } unit_costs = { "Concrete": 120, "Steel": 300, "Glass": 200 } def calculate_material_costs(quantities, costs): result = {} total = 0 for material in quantities: if material in costs: cost = quantities[material] * costs[material] result[material] = cost total += cost else: result[material] = "No unit cost available" result["Total"] = total return result cost_summary = calculate_material_costs(material_quantities, unit_costs) print(cost_summary) # Output: {'Concrete': 12000, 'Steel': 15000, 'Glass': 6000, 'Total': 33000}
copy

This pattern of combining two dictionariesโ€”one for quantities and one for unit pricesโ€”can be applied to various architectural calculations. By automating this process, you reduce the risk of manual errors and save time, especially when dealing with large projects or frequent updates to material prices. You also make it easy to adjust quantities or prices and instantly see the impact on the overall budget.

Task

Swipe to start coding

Write a function called material_cost_calculator that takes two dictionaries as arguments: quantities and unit_costs. The function should:

  • Create a new dictionary with the total cost for each material (quantity multiplied by unit cost).
  • Add a key "Total" with the sum of all material costs.
  • Return the resulting dictionary.

Test your function using the following inputs:

quantities = {"Brick": 500, "Wood": 250, "Glass": 100}
unit_costs = {"Brick": 2, "Wood": 5, "Glass": 10}

Your output should be:

{'Brick': 1000, 'Wood': 1250, 'Glass': 1000, 'Total': 3250}

Solution

Switch to desktopSwitch to desktop for real-world practiceContinue from where you are using one of the options below
Everything was clear?

How can we improve it?

Thanks for your feedback!

Sectionย 1. Chapterย 5
single

single

Ask AI

expand

Ask AI

ChatGPT

Ask anything or try one of the suggested questions to begin our chat

some-alt