diff options
| author | Christian Kolset <christian.kolset@gmail.com> | 2025-04-25 11:44:11 -0600 |
|---|---|---|
| committer | Christian Kolset <christian.kolset@gmail.com> | 2025-04-25 11:44:11 -0600 |
| commit | 169c98613442fbabb60f6b4792454b7598f65e80 (patch) | |
| tree | b85675619166612b916d9cffc319e0995dceb75b | |
| parent | c98972ced700b6250915a21af4b76459365743f3 (diff) | |
Updated control_structures notebook
| -rw-r--r-- | README.md | 2 | ||||
| -rw-r--r-- | book/computingME.pdf | bin | 3765279 -> 3596141 bytes | |||
| -rw-r--r-- | book/module1/module1.tex | 2 | ||||
| -rw-r--r-- | tutorials/module_1/notebook_1/control_structures.ipynb | 245 |
4 files changed, 213 insertions, 36 deletions
@@ -1,4 +1,4 @@ -# ComputingME + 21# ComputingME Development repository for MECH 305 - Computing for Mechanical Engineers ## Administive diff --git a/book/computingME.pdf b/book/computingME.pdf Binary files differindex 8cbe696..03e0722 100644 --- a/book/computingME.pdf +++ b/book/computingME.pdf diff --git a/book/module1/module1.tex b/book/module1/module1.tex index 6ed476b..5fd38b9 100644 --- a/book/module1/module1.tex +++ b/book/module1/module1.tex @@ -1,6 +1,4 @@ \chapter{Module 1: Introductory Programming Concepts} -\input{module1/intro_to_programming} -\input{module1/installing_anaconda} \input{module1/intro_to_anaconda} \input{module1/jupyter_lab_notebook} \input{module1/spyder_getting_started} diff --git a/tutorials/module_1/notebook_1/control_structures.ipynb b/tutorials/module_1/notebook_1/control_structures.ipynb index ce69e3a..dc40faa 100644 --- a/tutorials/module_1/notebook_1/control_structures.ipynb +++ b/tutorials/module_1/notebook_1/control_structures.ipynb @@ -2,6 +2,7 @@ "cells": [ { "cell_type": "markdown", + "id": "3a3edcbf-d972-4802-b3cf-d0e1f502bc83", "metadata": {}, "source": [ "# Control Structures\n", @@ -55,9 +56,45 @@ "Sometimes, we need to check conditions within other conditions. This is\n", "known as **nesting**. For example, if we first determine that a person\n", "is an adult, we can then check if they are a student. Based on that\n", - "information, we print different messages.\n", - "\n", - "``` python\n", + "information, we print different messages." + ] + }, + { + "cell_type": "code", + "execution_count": 1, + "id": "0e1ddca8-50e7-42f9-980d-a11553bedd0d", + "metadata": {}, + "outputs": [ + { + "name": "stdin", + "output_type": "stream", + "text": [ + "Enter the student's score (0-100): 3\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "The student has failed.\n" + ] + }, + { + "name": "stdin", + "output_type": "stream", + "text": [ + "Is the student eligible for a retest? (yes/no): no\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "The student has failed the course and must retake it next semester.\n" + ] + } + ], + "source": [ "# Getting user input for the student's score\n", "score = int(input(\"Enter the student's score (0-100): \"))\n", "\n", @@ -81,13 +118,14 @@ " if retake_eligible == \"yes\":\n", " print(\"The student is eligible for a retest.\")\n", " else:\n", - " print(\"The student has failed the course and must retake it next semester.\")\n", - "\n", - " \n", - "```\n", - "\n", - "------------------------------------------------------------------------\n", - "\n", + " print(\"The student has failed the course and must retake it next semester.\")" + ] + }, + { + "cell_type": "markdown", + "id": "445cdeba-eaa7-4498-b58e-c61d274f6cea", + "metadata": {}, + "source": [ "## Loops in Python\n", "\n", "Loops allow a program to execute a block of code multiple times. This is\n", @@ -110,21 +148,63 @@ "\n", "Additionally, the `enumerate()` function can be used to loop through a\n", "list while keeping track of the index of each item. This is useful when\n", - "both the position and the value in a sequence are needed.\n", - "\n", - "``` python\n", + "both the position and the value in a sequence are needed." + ] + }, + { + "cell_type": "code", + "execution_count": 2, + "id": "0247beed-26d2-4e87-b515-acac559414f8", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "apple\n", + "banana\n", + "cherry\n" + ] + } + ], + "source": [ "fruits = [\"apple\", \"banana\", \"cherry\"] \n", "for x in fruits: \n", - " print(x)\n", - "```\n", - "\n", - "``` python\n", + " print(x)" + ] + }, + { + "cell_type": "code", + "execution_count": 3, + "id": "5be4f60f-13a1-4721-9290-2d8a1bc70db0", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "0\n", + "1\n", + "2\n", + "3\n", + "4\n", + "5\n", + "Finally finished!\n" + ] + } + ], + "source": [ "for x in range(6): \n", - " print(x) \n", + " print(x) \n", "else: \n", - " print(\"Finally finished!\")\n", - "```\n", - "\n", + " print(\"Finally finished!\")" + ] + }, + { + "cell_type": "markdown", + "id": "762ddf23-0ecd-491e-93ba-1e861e9e73bf", + "metadata": {}, + "source": [ "### The `while` Loop\n", "\n", "Unlike `for` loops, which iterate over a sequence, `while` loops\n", @@ -142,17 +222,39 @@ "A `while` loop can also be used to wait for a certain event to occur.\n", "For example, in interactive programs, a `while True` loop can keep\n", "running until the user provides a valid input, at which point we break\n", - "out of the loop.\n", - "\n", - "``` python\n", + "out of the loop." + ] + }, + { + "cell_type": "code", + "execution_count": 4, + "id": "05ef7dd1-e52f-4656-97a3-837e7aa5a9a1", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "1\n", + "2\n", + "3\n", + "4\n", + "5\n" + ] + } + ], + "source": [ "i = 1 \n", "while i < 6: \n", - " print(i) \n", - " i += 1\n", - "```\n", - "\n", - "------------------------------------------------------------------------\n", - "\n", + " print(i) \n", + " i += 1" + ] + }, + { + "cell_type": "markdown", + "id": "117cd48e-c944-41f5-aa50-258ff8bf0e1e", + "metadata": {}, + "source": [ "## Loop Control Statements\n", "\n", "Python provides special statements to control the behavior of loops.\n", @@ -188,11 +290,88 @@ "\n", "For example, in a loop where a condition has not yet been implemented,\n", "using `pass` ensures that the code remains valid while avoiding errors." + ] + }, + { + "cell_type": "markdown", + "id": "1a49130f-d996-4229-914f-7255301d9077", + "metadata": {}, + "source": [ + "# Excercise\n", + "\n", + "So far these examples are quite simple and straightforward. Let's apply this knowledge to programming a CNC \n", + "\n", + "Write a Python program that:\n", + "- Asks the user to input the current temperature of the machine.\n", + "- Uses if/elif/else statements to print the correct system response:\n", + " - If the temperature is below 40°C, print: \"Temperature too low. Warming system activated.\"\n", + " - If the temperature is between 40°C and 85°C, print: \"Temperature is within safe range.\"\n", + " - If the temperature is above 85°C but below 100°C, print: \"Warning: High temperature. Cooling fan activated.\"\n", + " - If the temperature is 100°C or above, print: \"CRITICAL: Shutting down to prevent damage.\"" + ] + }, + { + "cell_type": "code", + "execution_count": 6, + "id": "d551eacc-96a9-4c41-90d7-eb1d9da0f87d", + "metadata": {}, + "outputs": [ + { + "name": "stdin", + "output_type": "stream", + "text": [ + "Enter current temperature in Celsius: 80\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Temperature is within safe range.\n" + ] + } ], - "id": "bd5766da-a032-4dda-a018-e6fb3314f09b" + "source": [ + "# Temperature monitoring system for CNC machine\n", + "\n", + "# Get temperature input from user\n", + "temperature = float(input(\"Enter current temperature in Celsius: \"))\n", + "\n", + "# Check temperature conditions\n", + "if temperature < 40:\n", + " print(\"Temperature too low. Warming system activated.\")\n", + " HEAT = TRUE\n", + "elif 40 <= temperature <= 85:\n", + " print(\"Temperature is within safe range.\")\n", + "elif 85 < temperature < 100:\n", + " print(\"Warning: High temperature. Cooling fan activated.\")\n", + " fan = TRUE\n", + "else: # temperature >= 100\n", + " print(\"CRITICAL: Shutting down to prevent damage.\")\n", + " motor = FALSE\n", + " heat = FALSE\n" + ] } ], + "metadata": { + "kernelspec": { + "display_name": "Python 3 (ipykernel)", + "language": "python", + "name": "python3" + }, + "language_info": { + "codemirror_mode": { + "name": "ipython", + "version": 3 + }, + "file_extension": ".py", + "mimetype": "text/x-python", + "name": "python", + "nbconvert_exporter": "python", + "pygments_lexer": "ipython3", + "version": "3.12.9" + } + }, "nbformat": 4, - "nbformat_minor": 5, - "metadata": {} + "nbformat_minor": 5 } |
