Day 2 – Operators, Input/Output, and Type Castin


 📐 Day 2 – Operators, Input/Output, and Type Casting

1. Learning Objectives

By the end of Day 2, you will be able to:

  • Use arithmetic operators (+, -, *, /, //, %, **) in engineering calculations.
  • Use comparison operators (==, !=, <, >, <=, >=) to check design rules.
  • Get user input with input() (e.g., beam span, column load).
  • Convert between data types (type casting) to avoid common errors.
  • Write small interactive scripts for quick AEC checks.

2. Concept Explanation

2.1 Arithmetic Operators

OperatorNameAEC Example
+Additiontotal_length = span1 + span2
-Subtractionremaining_stock = stock - used
*Multiplicationarea = length * width
/Divisionstress = force / area
//Floor divisionbeams_needed = total_length // stock_length
%Moduluswaste = total_length % stock_length
**Exponentiationmoment_of_inertia = b * h**3 / 12

Important for AEC:

  • / always returns a float (e.g., 7 / 2 = 3.5).
  • // returns an integer (floor) – useful to find how many full lengths you can cut from a stock length.
  • % gives the remainder (waste).

2.2 Comparison Operators

OperatorMeaningAEC Check Example
==Equal toif u_value == target_u:
!=Not equal toif material != "Concrete":
<Less thanif deflection < span/250:
>Greater thanif load > capacity:
<=Less than or equalif depth <= max_depth:
>=Greater than or equalif reinforcement_area >= required:

2.3 User Input (input())

input(prompt) reads a line from the user and returns it as a string.

beam_span_str = input("Enter beam span in metres: ")

You must convert the string to a number before calculations (see next section).

2.4 Type Casting

Casting converts one data type to another:

FunctionConverts toExample
int()integerint("5")5
float()floating‑pointfloat("4.25")4.25
str()stringstr(10)"10"

Why cast?
If you try "5" + 3, Python raises a TypeError.
Always convert input strings before arithmetic.


3. Code Examples

Example 1: Interactive beam length check

# Get input from user
span_str = input("Enter beam span (m): ")
stock_str = input("Enter standard stock length (m): ")

# Convert to float
span = float(span_str)
stock = float(stock_str)

# Check if span exceeds stock length
if span > stock:
    print("WARNING: Beam span exceeds standard stock length.")
else:
    print("OK: Beam span is within stock length.")

# Also compute how many full lengths needed (floor division)
full_lengths = int(span // stock)
remainder = span % stock
print(f"Full stock pieces needed: {full_lengths}")
print(f"Waste/extra per piece: {remainder:.2f} m")

Example 2: Floor area and material cost

# Interactive slab cost estimator
print("=== SLAB COST ESTIMATOR ===")
length = float(input("Slab length (m): "))
width  = float(input("Slab width (m): "))
thickness = float(input("Thickness (m) [e.g. 0.2]: "))
rate   = float(input("Concrete rate per m³ ($): "))

volume = length * width * thickness
cost   = volume * rate

print(f"\nVolume: {volume:.2f} m³")
print(f"Estimated cost: ${cost:.2f}")

Example 3: Comparison for code compliance

# U-value compliance
target_u = 0.28
actual_u = float(input("Enter wall U-value (W/m²K): "))

if actual_u <= target_u:
    print("PASS: U-value meets building code requirement.")
else:
    print("FAIL: U-value exceeds maximum allowed.")

4. Hands-on Exercises (3-5 Problems)

Try each problem before looking at the solutions.

Problem 1 – Beam waste calculator
Write a script that:

  • Asks for total_beam_length (m) and stock_length (m).
  • Calculates how many full stock pieces are needed and the leftover waste.
  • Prints: "Pieces: X, Waste: Y m"

Problem 2 – Load vs capacity check
A steel column has a design capacity of 1200 kN.
Ask the user to enter the applied load (kN).
Print "COLUMN OK" if load ≤ capacity, else "COLUMN OVERSTRESSED".

Problem 3 – Concrete mix ratio
A concrete mix uses 1:2:4 (cement:sand:aggregate).
Ask for the total volume of concrete needed (m³).
Compute:

  • cement_vol = total / 7 (since 1+2+4=7)
  • sand_vol = 2 * cement_vol
  • aggregate_vol = 4 * cement_vol
    Print each volume with 2 decimal places.

Problem 4 – Average floor height
Ask the user to enter the heights (in m) of three floors (as three separate inputs).
Compute and print the average height (float).

Solutions (only after trying):

# P1
total = float(input("Total beam length (m): "))
stock = float(input("Stock length (m): "))
pieces = int(total // stock)
waste = total % stock
print(f"Pieces: {pieces}, Waste: {waste:.2f} m")

# P2
capacity = 1200.0
load = float(input("Applied load (kN): "))
if load <= capacity:
    print("COLUMN OK")
else:
    print("COLUMN OVERSTRESSED")

# P3
total_vol = float(input("Total concrete volume (m³): "))
cement = total_vol / 7
sand = 2 * cement
agg = 4 * cement
print(f"Cement: {cement:.2f} m³")
print(f"Sand: {sand:.2f} m³")
print(f"Aggregate: {agg:.2f} m³")

# P4
h1 = float(input("Floor 1 height (m): "))
h2 = float(input("Floor 2 height (m): "))
h3 = float(input("Floor 3 height (m): "))
avg = (h1 + h2 + h3) / 3
print(f"Average height: {avg:.2f} m")

5. Applied Challenge Task

Task: Interactive Steel Beam Designer
Write a script that helps a designer select a standard universal beam (UB) for a simply supported beam with a uniformly distributed load.

Given:

  • Standard UB depths available (in mm): 203, 254, 305, 356, 406, 457, 533, 610, 686, 762, 838, 914.
  • Maximum allowable deflection = span / 250.

The script should:

  1. Ask the user for:

    • Beam span (m).
    • Applied load (kN/m) – uniform.
  2. Approximate the required depth using a simplified rule:
    required_depth_mm = 0.02 * (load * span**2) (purely illustrative, not for real design).

  3. Determine the smallest standard UB depth that is ≥ required depth.

  4. Print the selected UB designation (e.g., "UB 406 x 178 x 60" – just depth is enough for now).

  5. Also check if deflection would be acceptable using this rough formula:
    actual_deflection_mm = (load * span**4) / (10.0) (again, simplified example).
    Adjust the factor (10.0) to make it realistic? Let’s use:
    actual_deflection = 5 * (load * 1000) * (span * 1000)**4 / (384 * 200000 * (depth**3/12))
    Wait, that’s too advanced. Keep it simple:
    deflection = (5 * load * span**4) / (384 * 200000 * (depth**3/12))
    but that uses units. Let’s just use a heuristic:

For the challenge, we’ll keep it very simple:

  • Compute a deflection check factor = load * span (kN·m).
  • If factor < 100, print "Deflection likely OK", else "Deflection may be critical – consider deeper section".

This teaches input, casting, conditionals, and loops (maybe a simple for or while to find the appropriate depth – but we haven’t covered loops yet, so use a pre‑defined list and if-elif chain, or a manually written sequence).

Alternative simpler challenge:

Simplified version (recommended for Day 2):
Write a script that asks for the room length, width, and number of floors. Compute the total floor area. Then ask for the carpet roll width (m) and calculate how many linear metres of carpet are needed if the carpet is laid parallel to the room width. Use // and % to account for waste.

For now, we’ll offer the carpet challenge as it uses only Day 2 concepts.


6. Brief Review Summary

  • Arithmetic operators: +, -, *, /, //, %, **.
  • Comparison operators: ==, !=, <, >, <=, >=.
  • User input with input() returns a string – must cast to number.
  • Type casting: int(), float(), str().
  • Practical checks: stock lengths, code compliance, area/volume calculations.

Key takeaway:
You can now write interactive scripts that take real‑world inputs, perform checks, and give immediate feedback – a fundamental step toward automating AEC design decisions.


7. Preview of Next Topic (Day 3)

Tomorrow we’ll dive into conditional statements and Boolean logic in depth.
You’ll learn:

  • if / elif / else chains for multi‑condition decisions.
  • and, or, not for complex code compliance checks.
  • How to write robust design rule checks (e.g., “beam depth must be between 200 mm and 600 mm and deflection < L/250”).
  • A mini‑exercise: validating a building element against multiple code clauses.

Get ready to model real design constraints!

Comments

EARN AT THE COMFORT OF YOUR HOME

Sponsored content

Popular posts from this blog

Day 5 – Functions, Scope, and Docstrings

Day 10 – Exception Handling and Debugging

Day 1 – Python Foundations for AEC Professionals