Skip to content

Commit 79c8a87

Browse files
edoardob90despadampre-commit-ci[bot]
authored
Fix errors found in Functions (#273)
* Exercise 2.3: checks for unsupported units * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * fix units and decimals in ex2 --------- Co-authored-by: Adamopoulou <despoina.adamopoulou@empa.ch> Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
1 parent 75b8464 commit 79c8a87

File tree

2 files changed

+31
-7
lines changed

2 files changed

+31
-7
lines changed

03_functions.ipynb

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -489,7 +489,7 @@
489489
"\n",
490490
"*Hint:* you can use the [built-in function](https://docs.python.org/3/library/functions.html#round) `round(number, ndigits)` function to round the floating point `number` to a given number expressed by `ndigits`.\n",
491491
"\n",
492-
"Example: `length=2`, `width=3` should return `\"6.00 cm^2\"`\n",
492+
"Example: `length=1.5`, `width=2.57` should return `\"3.85 cm^2\"`\n",
493493
"\n",
494494
"<div class=\"alert alert-block alert-warning\">\n",
495495
" <h4><b>Note</b></h4>\n",
@@ -515,7 +515,7 @@
515515
"\n",
516516
" Returns:\n",
517517
" - A string representing the area with 2 decimal places,\n",
518-
" followed by \"cm^2\" (e.g., \"6.00 cm^2\")\n",
518+
" followed by \"cm^2\" (e.g., \"3.85 cm^2\")\n",
519519
" \"\"\"\n",
520520
" return"
521521
]
@@ -528,12 +528,14 @@
528528
"### Part 2\n",
529529
"\n",
530530
"Extend the previous function, now called `solution_calculate_metric_area`, by adding a `unit` parameter that can be either \"cm\" or \"m\".\n",
531+
"The default `length` and `width` unit is centimeters (\"cm\").\n",
532+
"\n",
531533
"If the unit is \"m\", convert the measurements to centimeters before calculating the area.\n",
532534
"The result should still be in cm^2.\n",
533535
"\n",
534536
"*Hint:* remember the [built-in function](https://docs.python.org/3/library/functions.html#round) `round(number, ndigits)`.\n",
535537
"\n",
536-
"Example: `length=2`, `width=3`, `unit=\"m\"` should return `\"60000.00 cm^2\"` (because 2m × 3m = 6m² = 60000cm²)\n",
538+
"Example: `length=2.0`, `width=3.0`, `unit=\"m\"` should return `\"60000.0 cm^2\"` (because 2m × 3m = 6m² = 60000cm²)\n",
537539
"\n",
538540
"<div class=\"alert alert-block alert-warning\">\n",
539541
" <h4><b>Note</b></h4>\n",
@@ -573,7 +575,7 @@
573575
"### Part 3\n",
574576
"\n",
575577
"Extend the previous function, now called `solution_calculate_area`, to support the following units: cm, m, mm, yd, and ft.\n",
576-
"The calculated area should always be in centimeters, so you need to take care of the appropriate conversions (when needed).\n",
578+
"The calculated area should always be in cm^2, so you need to take care of the appropriate conversions (when needed).\n",
577579
"Keep the same string format for the result.\n",
578580
"\n",
579581
"Use the following conversion factors:\n",

tutorial/tests/test_03_functions.py

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -95,10 +95,10 @@ def validate_basic_area_signature(function_to_test) -> None:
9595
@pytest.mark.parametrize(
9696
"length,width",
9797
[
98-
(2.0, 3.0),
98+
(2.123, 3.456),
9999
(5.0, 4.0),
100-
(1.5, 2.5),
101-
(0.1, 0.1),
100+
(1.5, 2.57),
101+
(0.109, 0.103),
102102
],
103103
)
104104
def test_calculate_basic_area(length: float, width: float, function_to_test):
@@ -234,6 +234,28 @@ def test_calculate_area(length, width, unit, function_to_test):
234234
assert expected == result, "Incorrect area calculation or formatting"
235235

236236

237+
@pytest.mark.parametrize(
238+
"length,width,unit",
239+
[
240+
(2.0, 3.0, "km"), # Invalid unit
241+
(2.0, 3.0, ""), # Empty unit
242+
(2.0, 3.0, "CM"), # Case sensitive check
243+
(2.0, 3.0, "inches"), # Another invalid unit
244+
],
245+
)
246+
def test_calculate_area_invalid_units(length, width, unit, function_to_test):
247+
"""Test that the function properly handles invalid units."""
248+
validate_area_signature(function_to_test)
249+
result = function_to_test(length, width, unit)
250+
expected = f"Invalid unit: {unit}"
251+
assert isinstance(result, str), (
252+
"Result should be a string like 'Invalid unit: <unit>'"
253+
)
254+
assert result == expected, (
255+
f"Expected '{expected}' for invalid unit '{unit}', got '{result}'"
256+
)
257+
258+
237259
#
238260
# Exercise 3: summing anything
239261
#

0 commit comments

Comments
 (0)