Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Lära Challenge: Material Cost Calculator | Architectural Data Analysis with Python
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.

Uppgift

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}

Lösning

Var allt tydligt?

Hur kan vi förbättra det?

Tack för dina kommentarer!

Avsnitt 1. Kapitel 5
single

single

Fråga AI

expand

Fråga AI

ChatGPT

Fråga vad du vill eller prova någon av de föreslagna frågorna för att starta vårt samtal

Suggested prompts:

Can you explain how the function handles missing unit costs for a material?

What would happen if a material is present in unit_costs but not in material_quantities?

Can you show how to modify the function to handle decimal values for costs and quantities?

close

bookChallenge: Material Cost Calculator

Svep för att visa menyn

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.

Uppgift

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}

Lösning

Switch to desktopByt till skrivbordet för praktisk övningFortsätt där du är med ett av alternativen nedan
Var allt tydligt?

Hur kan vi förbättra det?

Tack för dina kommentarer!

Avsnitt 1. Kapitel 5
single

single

some-alt