Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Leer Challenge: Material Cost Calculator | Architectural Data Analysis with Python
Practice
Projects
Quizzes & Challenges
Quizzes
Challenges
/
Python for Architects

bookChallenge: Material Cost Calculator

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.

Taak

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}

Oplossing

Was alles duidelijk?

Hoe kunnen we het verbeteren?

Bedankt voor je feedback!

Sectie 1. Hoofdstuk 5
single

single

Vraag AI

expand

Vraag AI

ChatGPT

Vraag wat u wilt of probeer een van de voorgestelde vragen om onze chat te starten.

close

bookChallenge: Material Cost Calculator

Veeg om het menu te tonen

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.

Taak

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}

Oplossing

Switch to desktopSchakel over naar desktop voor praktijkervaringGa verder vanaf waar je bent met een van de onderstaande opties
Was alles duidelijk?

Hoe kunnen we het verbeteren?

Bedankt voor je feedback!

Sectie 1. Hoofdstuk 5
single

single

some-alt