diff --git a/f23/Gurmail_Lecture_Notes/25_Error_Handling/lec_25_error_handling_template_Gurmail_lec1.ipynb b/f23/Gurmail_Lecture_Notes/25_Error_Handling/lec_25_error_handling_template_Gurmail_lec1.ipynb index 1790aa918ad8d271b9af061685abc0ae12809c40..52cb667045d4575263a8555d488bcb4ccadfa147 100644 --- a/f23/Gurmail_Lecture_Notes/25_Error_Handling/lec_25_error_handling_template_Gurmail_lec1.ipynb +++ b/f23/Gurmail_Lecture_Notes/25_Error_Handling/lec_25_error_handling_template_Gurmail_lec1.ipynb @@ -274,14 +274,14 @@ "# Copy pasted code (not a typical thing which I will ask you to do) \n", "# change function definitions to v2 and add the assert statements\n", "\n", - "def pizza_size(radius):\n", + "def pizza_size_v2(radius):\n", " return (radius ** 2) * math.pi\n", "\n", - "def slice_size(radius, slice_count):\n", + "def slice_size_v2(radius, slice_count):\n", " total_size = pizza_size(radius)\n", " return total_size * (1 / slice_count)\n", "\n", - "def main():\n", + "def main_v2():\n", " for i in range(3):\n", " # grab input\n", " args = input(\"Enter pizza diameter(inches), slice count: \")\n", @@ -551,7 +551,7 @@ "# Copy pasted code (not a typical thing which I will ask you to do) \n", "# change function definitions to v3 and add the try / except blocks\n", "\n", - "def main_v2():\n", + "def main_v3():\n", " for i in range(3):\n", " # grab input\n", " args = input(\"Enter pizza diameter(inches), slice count: \")\n", @@ -605,7 +605,7 @@ "source": [ "# Let's create an intentional SyntaxError\n", "\n", - "def main_v3():\n", + "def main_v4():\n", " for i in range(5):\n", " try:\n", " # grab input\n", @@ -661,7 +661,7 @@ "# Let's fix our except blocks\n", "# Try introducing the intentional SyntaxError now\n", "\n", - "def main_v3():\n", + "def main_v5():\n", " for i in range(5):\n", " try:\n", " # grab input\n", @@ -763,17 +763,22 @@ "# Copy pasted code (not a typical thing which I will ask you to do) \n", "# change function definitions to v2 and add the assert statements\n", "\n", - "def pizza_size_v2(radius):\n", - " assert radius >= 0\n", + "def pizza_size_v3(radius):\n", + " if radius <= 0:\n", + " raise ArithmeticError(\"Invalid radius size!\")\n", " return (radius ** 2) * math.pi\n", "\n", - "def slice_size_v2(radius, slice_count):\n", - " assert slice_count >= 0\n", - " total_size = pizza_size_v2(radius)\n", + "def slice_size_v3(radius, slice_count):\n", + " if slice_count < 0:\n", + " raise ArithmeticError(\"Invalid slice count!\")\n", + " total_size = pizza_size_v3(radius)\n", " return total_size * (1 / slice_count)\n", "\n", - "def main_v5():\n", - " for i in range(5):\n", + "# Let's fix our except blocks\n", + "# Try introducing the intentional SyntaxError now\n", + "\n", + "def main_v6():\n", + " for i in range(3):\n", " # grab input\n", " try:\n", " args = input(\"Enter pizza diameter(inches), slice count: \")\n", @@ -787,10 +792,10 @@ "\n", " # pizza analysis\n", " try:\n", - " size = slice_size_v2(radius, slices)\n", + " size = slice_size_v3(radius, slices)\n", " print('PIZZA: radius={}, slices={}, slice square inches={}'\n", " .format(radius, slices, size))\n", - " except (ZeroDivisionError, AssertionError) as e:\n", + " except (ZeroDivisionError, ArithmeticError) as e:\n", " print(\"Pizza analysis error!\", str(e))\n", " print(\"Type of exception:\", type(e))" ] diff --git a/f23/Gurmail_Lecture_Notes/25_Error_Handling/lec_25_error_handling_template_Gurmail_lec2.ipynb b/f23/Gurmail_Lecture_Notes/25_Error_Handling/lec_25_error_handling_template_Gurmail_lec2.ipynb index 1790aa918ad8d271b9af061685abc0ae12809c40..52cb667045d4575263a8555d488bcb4ccadfa147 100644 --- a/f23/Gurmail_Lecture_Notes/25_Error_Handling/lec_25_error_handling_template_Gurmail_lec2.ipynb +++ b/f23/Gurmail_Lecture_Notes/25_Error_Handling/lec_25_error_handling_template_Gurmail_lec2.ipynb @@ -274,14 +274,14 @@ "# Copy pasted code (not a typical thing which I will ask you to do) \n", "# change function definitions to v2 and add the assert statements\n", "\n", - "def pizza_size(radius):\n", + "def pizza_size_v2(radius):\n", " return (radius ** 2) * math.pi\n", "\n", - "def slice_size(radius, slice_count):\n", + "def slice_size_v2(radius, slice_count):\n", " total_size = pizza_size(radius)\n", " return total_size * (1 / slice_count)\n", "\n", - "def main():\n", + "def main_v2():\n", " for i in range(3):\n", " # grab input\n", " args = input(\"Enter pizza diameter(inches), slice count: \")\n", @@ -551,7 +551,7 @@ "# Copy pasted code (not a typical thing which I will ask you to do) \n", "# change function definitions to v3 and add the try / except blocks\n", "\n", - "def main_v2():\n", + "def main_v3():\n", " for i in range(3):\n", " # grab input\n", " args = input(\"Enter pizza diameter(inches), slice count: \")\n", @@ -605,7 +605,7 @@ "source": [ "# Let's create an intentional SyntaxError\n", "\n", - "def main_v3():\n", + "def main_v4():\n", " for i in range(5):\n", " try:\n", " # grab input\n", @@ -661,7 +661,7 @@ "# Let's fix our except blocks\n", "# Try introducing the intentional SyntaxError now\n", "\n", - "def main_v3():\n", + "def main_v5():\n", " for i in range(5):\n", " try:\n", " # grab input\n", @@ -763,17 +763,22 @@ "# Copy pasted code (not a typical thing which I will ask you to do) \n", "# change function definitions to v2 and add the assert statements\n", "\n", - "def pizza_size_v2(radius):\n", - " assert radius >= 0\n", + "def pizza_size_v3(radius):\n", + " if radius <= 0:\n", + " raise ArithmeticError(\"Invalid radius size!\")\n", " return (radius ** 2) * math.pi\n", "\n", - "def slice_size_v2(radius, slice_count):\n", - " assert slice_count >= 0\n", - " total_size = pizza_size_v2(radius)\n", + "def slice_size_v3(radius, slice_count):\n", + " if slice_count < 0:\n", + " raise ArithmeticError(\"Invalid slice count!\")\n", + " total_size = pizza_size_v3(radius)\n", " return total_size * (1 / slice_count)\n", "\n", - "def main_v5():\n", - " for i in range(5):\n", + "# Let's fix our except blocks\n", + "# Try introducing the intentional SyntaxError now\n", + "\n", + "def main_v6():\n", + " for i in range(3):\n", " # grab input\n", " try:\n", " args = input(\"Enter pizza diameter(inches), slice count: \")\n", @@ -787,10 +792,10 @@ "\n", " # pizza analysis\n", " try:\n", - " size = slice_size_v2(radius, slices)\n", + " size = slice_size_v3(radius, slices)\n", " print('PIZZA: radius={}, slices={}, slice square inches={}'\n", " .format(radius, slices, size))\n", - " except (ZeroDivisionError, AssertionError) as e:\n", + " except (ZeroDivisionError, ArithmeticError) as e:\n", " print(\"Pizza analysis error!\", str(e))\n", " print(\"Type of exception:\", type(e))" ]