Keyboard shortcuts

Press ← or → to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Antwoorden — Hoofdstuk 1: Variabelen en basisberekeningen

Opmerking: Code is in het Engels, met korte Nederlandse toelichting erboven.

Antwoord 1 — Cirkel: omtrek en oppervlakte

  • We lezen een float, gebruiken math.pi, en tonen resultaten met f-strings.
import math

raw = input("Enter radius: ")
radius = float(raw)

circumference = 2 * math.pi * radius
area = math.pi * (radius ** 2)

print(f"Circumference: {circumference}")
print(f"Area: {area}")

Met afronding (optioneel):

print(f"Circumference: {circumference:.2f}")
print(f"Area: {area:.2f}")

Antwoord 2 — BMI met afronding

  • We vragen lengte en gewicht; ronden af op 1 decimaal in de uitvoer.
height_m = float(input("Enter height in meters: "))
weight_kg = float(input("Enter weight in kg: "))

bmi = weight_kg / (height_m ** 2)
print(f"BMI is {bmi:.1f}")

Antwoord 3 — BTW 9%

  • We gebruiken een variabele voor het percentage en ronden 2 decimalen.
amount = float(input("Amount (ex VAT): "))
vat_rate = 0.09
price_inc_vat = amount * (1 + vat_rate)
print(f"Price incl VAT: {price_inc_vat:.2f}")

Antwoord 4 — Variabelen ruilen (swap)

  • Twee varianten: met tijdelijke variabele, en met tuple-swap.
# With a temporary variable
a = 3
b = 7
print("Before:", a, b)

tmp = a
a = b
b = tmp
print("After:", a, b)
# Pythonic tuple swap
a = 3
b = 7
print("Before:", a, b)

a, b = b, a
print("After:", a, b)

Antwoord 5 — Kortingen stapelen

  • Eerst 10% en dan 5% is niet exact gelijk aan één keer 15%.
original = 80.0

first = original * 0.90   # 10% off
second = first * 0.95     # then 5% off

single = original * 0.85  # single 15% off

print(f"Stacked discounts: {second:.2f}")
print(f"Single 15%: {single:.2f}")
print("Equal?", abs(second - single) < 1e-9)

Antwoord 6 — Kommagetallen en precisie

  • 0.1 + 0.2 geeft een benadering; we tonen ook afgerond.
print(0.1 + 0.2)          # likely 0.30000000000000004
print((0.1 + 0.2) == 0.3) # False
print(round(0.1 + 0.2, 2))

Uitdaging (optioneel) — Rechthoek helper

width = float(input("Width: "))
height = float(input("Height: "))

area = width * height
perimeter = 2 * (width + height)

print(f"Area: {area:.2f}")
print(f"Perimeter: {perimeter:.2f}")