|
3746 | 3746 | "cell_type": "code", |
3747 | 3747 | "execution_count": null, |
3748 | 3748 | "id": "3a8f93ee", |
3749 | | - "metadata": {}, |
| 3749 | + "metadata": { |
| 3750 | + "remove_code": "after:# Modify the function to make an empty list return 0" |
| 3751 | + }, |
3750 | 3752 | "outputs": [], |
3751 | 3753 | "source": [ |
3752 | 3754 | "def average(numbers):\n", |
|
3755 | 3757 | "print(average([1,2,3,4]))\n", |
3756 | 3758 | "print(average([]))\n", |
3757 | 3759 | "\n", |
3758 | | - "# Modify the function to make an empty list return 0\n", |
3759 | | - "def average_result(numbers):\n", |
3760 | | - " try:\n", |
3761 | | - " return sum(numbers) / len(numbers)\n", |
3762 | | - " except ZeroDivisionError:\n", |
3763 | | - " return 0" |
| 3760 | + "# Modify the function to make an empty list return 0\n" |
3764 | 3761 | ] |
3765 | 3762 | }, |
3766 | 3763 | { |
|
3783 | 3780 | "id": "6a7114a0", |
3784 | 3781 | "metadata": { |
3785 | 3782 | "editable": true, |
| 3783 | + "remove_code": "after:# Modify function to list the index and content of the entry that caused the error", |
3786 | 3784 | "slideshow": { |
3787 | 3785 | "slide_type": "" |
3788 | 3786 | }, |
|
3804 | 3802 | "temperatures_with_error = [\"23.5\", \"25.1\", \"2020-05-21\", \"24.0\"]\n", |
3805 | 3803 | "print(parsed_max(temperatures_with_error))\n", |
3806 | 3804 | "\n", |
3807 | | - "# Modify function to list the index and content of the entry that caused the error\n", |
3808 | | - "def parsed_max_result(values):\n", |
3809 | | - " max_value = None\n", |
3810 | | - " for i, value in enumerate(values):\n", |
3811 | | - " try:\n", |
3812 | | - " parsed_value = float(value)\n", |
3813 | | - " if max_value is None or parsed_value > max_value:\n", |
3814 | | - " max_value = parsed_value\n", |
3815 | | - " except ValueError as e:\n", |
3816 | | - " raise ValueError(f\"Determining maximum: Could not convert '{value}' to float at index {i}\") from e\n", |
3817 | | - " return max_value\n" |
| 3805 | + "# Modify function to list the index and content of the entry that caused the error\n" |
3818 | 3806 | ] |
3819 | 3807 | }, |
3820 | 3808 | { |
|
3835 | 3823 | "cell_type": "code", |
3836 | 3824 | "execution_count": null, |
3837 | 3825 | "id": "681ad2e5", |
3838 | | - "metadata": {}, |
| 3826 | + "metadata": { |
| 3827 | + "remove_code": "after:# Modify function to check for negative depth" |
| 3828 | + }, |
3839 | 3829 | "outputs": [], |
3840 | 3830 | "source": [ |
3841 | 3831 | "def water_pressure(depth_m):\n", |
|
3847 | 3837 | "print(water_pressure(10))\n", |
3848 | 3838 | "print(water_pressure(-5))\n", |
3849 | 3839 | "\n", |
3850 | | - "# Modify function to check for negative depth\n", |
3851 | | - "def water_pressure_result(depth_m):\n", |
3852 | | - " if depth_m < 0:\n", |
3853 | | - " raise ValueError(\"Water depth cannot be negative\")\n", |
3854 | | - " g = 9.81 # acceleration due to gravity in m/s^2\n", |
3855 | | - " density = 1000 # density of water in kg/m^3\n", |
3856 | | - " pressure = density * g * depth_m\n", |
3857 | | - " return pressure\n" |
| 3840 | + "# Modify function to check for negative depth\n" |
3858 | 3841 | ] |
3859 | 3842 | }, |
3860 | 3843 | { |
|
3888 | 3871 | "id": "6401f075", |
3889 | 3872 | "metadata": { |
3890 | 3873 | "editable": true, |
| 3874 | + "remove_code": "after:# Modify function to always include the timing", |
3891 | 3875 | "slideshow": { |
3892 | 3876 | "slide_type": "" |
3893 | 3877 | }, |
|
3915 | 3899 | "\n", |
3916 | 3900 | "# Modify function to always include the timing\n", |
3917 | 3901 | "\n", |
3918 | | - "def timed_calculation_result(n):\n", |
3919 | | - " start_time = time.time()\n", |
3920 | | - " try:\n", |
3921 | 3902 | " # Some complex calculation\n", |
3922 | | - " if n < 0:\n", |
3923 | | - " raise ValueError(\"n must be positive\")\n", |
3924 | | - " \n", |
3925 | | - " result = sum(i**2 for i in range(n))\n", |
3926 | | - " return result\n", |
3927 | | - " finally:\n", |
3928 | | - " # Always log the execution time\n", |
3929 | | - " elapsed = time.time() - start_time\n", |
3930 | | - " print(f\"Calculation took {elapsed:.6f} seconds\")" |
| 3903 | + "\n", |
| 3904 | + " # Always log the execution time\n" |
3931 | 3905 | ] |
3932 | 3906 | }, |
3933 | 3907 | { |
|
3991 | 3965 | "cell_type": "code", |
3992 | 3966 | "execution_count": null, |
3993 | 3967 | "id": "d2151499", |
3994 | | - "metadata": {}, |
| 3968 | + "metadata": { |
| 3969 | + "remove_code": "after:# Modify the function to handle unexpected values" |
| 3970 | + }, |
3995 | 3971 | "outputs": [], |
3996 | 3972 | "source": [ |
3997 | 3973 | "def calculate_gpa(grades):\n", |
|
4005 | 3981 | "print(calculate_gpa([])) # Will cause ZeroDivisionError\n", |
4006 | 3982 | "print(calculate_gpa([3.5, \"A\", 3.7])) # Will cause TypeError\n", |
4007 | 3983 | "\n", |
4008 | | - "# Modify the function to handle unexpected values\n", |
4009 | | - "def calculate_gpa(grades):\n", |
4010 | | - " try:\n", |
4011 | | - " total_points = 0\n", |
4012 | | - " for i, grade in enumerate(grades):\n", |
4013 | | - " try:\n", |
4014 | | - " total_points += grade\n", |
4015 | | - " except TypeError:\n", |
4016 | | - " raise ValueError(f\"Invalid grade '{grade}' at position {i}. Expected numeric value.\") from None\n", |
4017 | | - " return total_points / len(grades)\n", |
4018 | | - " except ZeroDivisionError:\n", |
4019 | | - " return 0.0" |
| 3984 | + "# Modify the function to handle unexpected values\n" |
4020 | 3985 | ] |
4021 | 3986 | }, |
4022 | 3987 | { |
|
4440 | 4405 | "id": "fb0d76f6", |
4441 | 4406 | "metadata": { |
4442 | 4407 | "editable": true, |
| 4408 | + "remove_code": "all", |
4443 | 4409 | "slideshow": { |
4444 | 4410 | "slide_type": "" |
4445 | 4411 | }, |
4446 | 4412 | "tags": [] |
4447 | 4413 | }, |
4448 | 4414 | "outputs": [], |
4449 | 4415 | "source": [ |
4450 | | - "import sensor_api\n", |
4451 | | - "\n", |
4452 | | - "class PlantSensor:\n", |
4453 | | - " def __init__(self, sensor_id):\n", |
4454 | | - " \"\"\"Initialize the sensor with an ID and connect to it.\"\"\"\n", |
4455 | | - " self.sensor_id = sensor_id\n", |
4456 | | - " self.connected = sensor_api.connect(sensor_id)\n", |
4457 | | - " \n", |
4458 | | - " # Store readings\n", |
4459 | | - " self.soil_humidity = None\n", |
4460 | | - " self.air_humidity = None\n", |
4461 | | - " self.temperature = None\n", |
4462 | | - " \n", |
4463 | | - " def get_soil_humidity(self):\n", |
4464 | | - " \"\"\"Get soil humidity reading.\"\"\"\n", |
4465 | | - " if self.connected:\n", |
4466 | | - " message = f\"{self.sensor_id}:SOIL_HUMIDITY\"\n", |
4467 | | - " self.soil_humidity = float(sensor_api.send_message(message))\n", |
4468 | | - " return self.soil_humidity\n", |
4469 | | - " return None\n", |
4470 | | - " \n", |
4471 | | - " def get_air_humidity(self):\n", |
4472 | | - " \"\"\"Get air humidity reading.\"\"\"\n", |
4473 | | - " if self.connected:\n", |
4474 | | - " message = f\"{self.sensor_id}:AIR_HUMIDITY\"\n", |
4475 | | - " self.air_humidity = float(sensor_api.send_message(message))\n", |
4476 | | - " return self.air_humidity\n", |
4477 | | - " return None\n", |
4478 | | - " \n", |
4479 | | - " def get_temperature(self):\n", |
4480 | | - " \"\"\"Get temperature reading.\"\"\"\n", |
4481 | | - " if self.connected:\n", |
4482 | | - " message = f\"{self.sensor_id}:TEMPERATURE\"\n", |
4483 | | - " self.temperature = float(sensor_api.send_message(message))\n", |
4484 | | - " return self.temperature\n", |
4485 | | - " return None\n", |
4486 | | - " \n", |
4487 | | - " def display_readings(self):\n", |
4488 | | - " \"\"\"Display all sensor readings.\"\"\"\n", |
4489 | | - " print(f\"\\n=== Plant Sensor {self.sensor_id} Readings ===\")\n", |
4490 | | - " print(f\"Soil Humidity: {self.soil_humidity}%\")\n", |
4491 | | - " print(f\"Air Humidity: {self.air_humidity}%\")\n", |
4492 | | - " print(f\"Temperature: {self.temperature}\u00b0C\")\n", |
4493 | | - " print(\"=\" * 35)\n", |
4494 | | - " \n", |
4495 | | - " def disconnect(self):\n", |
4496 | | - " \"\"\"Disconnect from the sensor.\"\"\"\n", |
4497 | | - " if self.connected:\n", |
4498 | | - " sensor_api.disconnect(self.sensor_id)\n", |
4499 | | - " self.connected = False" |
| 4416 | + "\n" |
4500 | 4417 | ] |
4501 | 4418 | }, |
4502 | 4419 | { |
|
5865 | 5782 | ], |
5866 | 5783 | "metadata": { |
5867 | 5784 | "kernelspec": { |
5868 | | - "display_name": "Python 3 (ipykernel)", |
| 5785 | + "display_name": "teaching_data_analysis", |
5869 | 5786 | "language": "python", |
5870 | 5787 | "name": "python3" |
5871 | 5788 | }, |
|
0 commit comments