diff --git a/f24/Louis_Lecture_Notes/08_Conditionals1/Lec_08_Conditionals1.ipynb b/f24/Louis_Lecture_Notes/08_Conditionals1/Lec_08_Conditionals1.ipynb index 9f4e47df3fa16b22f4b90fe3ade39864f098a623..5788db0f073d90e5c128b666fadb3c0826638d42 100644 --- a/f24/Louis_Lecture_Notes/08_Conditionals1/Lec_08_Conditionals1.ipynb +++ b/f24/Louis_Lecture_Notes/08_Conditionals1/Lec_08_Conditionals1.ipynb @@ -251,6 +251,25 @@ " print(\"Word is long\")" ] }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### You Try\n", + "In the cell below print \"even\" if the inputed value is even" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "num = int(input(\"Enter an integer number\"))\n", + "\n", + "##TODO write an if statement that prints \"even\" if num is holding an even value\n" + ] + }, { "cell_type": "markdown", "metadata": {}, @@ -277,22 +296,24 @@ }, { "cell_type": "code", - "execution_count": 4, - "metadata": {}, + "execution_count": 3, + "metadata": { + "scrolled": true + }, "outputs": [ { "name": "stdin", "output_type": "stream", "text": [ - "How many balls of dough did we recieve? 3\n", - "How many customers were there? 5\n" + "How many balls of dough did we recieve? 4\n", + "How many customers were there? 4\n" ] }, { "name": "stdout", "output_type": "stream", "text": [ - "Each customer gets 2.4 loaves of bread.\n" + "Each customer gets 4.0 loaves of bread.\n" ] } ], @@ -309,6 +330,44 @@ " print(\"Each customer gets\", round(bread_per_customer, 2), \"loaves of bread.\")" ] }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### You Try\n", + "\n", + "In the cell below write an if-else statement that prints \"you may enter\" or \"entry denied\" depending\n", + "on the value in the `password` variable. The correct value for entry is **CatNipFOREVER**." + ] + }, + { + "cell_type": "code", + "execution_count": 2, + "metadata": {}, + "outputs": [ + { + "name": "stdin", + "output_type": "stream", + "text": [ + "Enter Password: ········\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "hello\n" + ] + } + ], + "source": [ + "from getpass import getpass\n", + "password = getpass(\"Enter Password:\")\n", + "\n", + "##TODO write an if-else statement that prints \"you may enter\" or \"entry denied\" if they entered\n", + "# the proper password (i.e. if password refers to the value \"CatNipFOREVER\")\n" + ] + }, { "cell_type": "markdown", "metadata": {}, @@ -397,7 +456,44 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "## Date Printer\n", + "### You Try\n", + "\n", + "In the cell below finish writing the `calculate_letter_grade()` function. The input to the function is a percentage (float) and the return value should be a letter grade following these rules:\n", + "\n", + "- \\>= 93 - A\n", + "- \\>= 88 - AB\n", + "- \\>= 80 - B\n", + "- \\>= 75 - BC\n", + "- \\>= 70 - C\n", + "- \\>= 60 - D\n", + "- anything else - F" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "##TODO finish the calculate_letter_grade() function\n", + "def calculate_letter_grade(percentage):\n", + " \"\"\"returns the letter grade for the provided course percentage\"\"\"\n", + " pass\n", + "\n", + "# Should work with the following function call. Try different values\n", + "print(calculate_letter_grade(86.5))" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Practice\n", + "\n", + "Let's work through computing a string representation of a date given the year, month, and day.\n", + "\n", + "\n", + "### Date Printer\n", "\n", "See the requirements below. We will first break down the problem into smaller problems and right a function for each." ] @@ -525,6 +621,20 @@ "print(format_date(1997, 10, 22))\n", "print(format_date(1497, 6, 8))" ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Summary\n", + "\n", + "You have learned about the syntax for the three types of conditional statements and you have practiced writing code using each of them.\n", + "\n", + "- **if** statement\n", + "- **if-else** statement\n", + "- **if-elif** statement\n", + "\n" + ] } ], "metadata": { @@ -543,7 +653,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.10.6" + "version": "3.10.12" } }, "nbformat": 4, diff --git a/f24/Louis_Lecture_Notes/08_Conditionals1/Lec_08_Conditionals1_Solution_Oliphant.ipynb b/f24/Louis_Lecture_Notes/08_Conditionals1/Lec_08_Conditionals1_Solution.ipynb similarity index 89% rename from f24/Louis_Lecture_Notes/08_Conditionals1/Lec_08_Conditionals1_Solution_Oliphant.ipynb rename to f24/Louis_Lecture_Notes/08_Conditionals1/Lec_08_Conditionals1_Solution.ipynb index e648a9260d7b03572c3b340f175e847b4a79aae6..666b1c5312af1a7b2311a7843700844ed89ae4ea 100644 --- a/f24/Louis_Lecture_Notes/08_Conditionals1/Lec_08_Conditionals1_Solution_Oliphant.ipynb +++ b/f24/Louis_Lecture_Notes/08_Conditionals1/Lec_08_Conditionals1_Solution.ipynb @@ -1,5 +1,12 @@ { "cells": [ + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Warmup" + ] + }, { "cell_type": "code", "execution_count": 1, @@ -26,7 +33,6 @@ " ans = balance*rate/num_payments\n", " return ans\n", "\n", - "\n", "# Function Calls that should work:\n", "# passing arguments by position\n", "print(interest_only_payment(50000, 0.04, 12)) # should return approx. $166.66\n", @@ -89,27 +95,9 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "# CS220: Lecture 08\n", - "\n", + "Warmup 3 --Trace the following code Using the debugger or Python Tutor. At any point as the program executes, you should be able to state what line of code will execute next and what the output will be.\n", "\n", - "## Learning Objectives\n", - "After this lecture you will be able to...\n", - " - write conditional statements\n", - " - using conditional execution ( if )\n", - " - using alternate execution (if/else)\n", - " - using chained conditionals (if/elif/elif/…/else)\n", - " - identify code blocks (indentation layers) in a program\n", - " - count the number of blocks in a segment of code\n", - " - determine the output of code with conditional statements" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "## Warmup\n", - "\n", - "Let's trace the following code (Practice Problem 3) [link](https://pythontutor.com/visualize.html#code=def%20f%28%29%3A%0A%20%20%20%20x%20%3D%201%0A%20%20%20%20print%28x%29%0A%20%20%20%20g%28%29%0A%0Adef%20g%28%29%3A%0A%20%20%20%20x%20%3D%202%0A%20%20%20%20print%28x%29%0A%20%20%20%20h%28%29%0A%0Adef%20h%28%29%3A%0A%20%20%20%20x%20%3D%203%0A%20%20%20%20print%28x%29%0A%0Af%28%29&cumulative=false&curInstr=0&heapPrimitives=nevernest&mode=display&origin=opt-frontend.js&py=3&rawInputLstJSON=%5B%5D&textReferences=false)..." + "[link](https://pythontutor.com/visualize.html#code=def%20f%28%29%3A%0A%20%20%20%20x%20%3D%201%0A%20%20%20%20print%28x%29%0A%20%20%20%20g%28%29%0A%0Adef%20g%28%29%3A%0A%20%20%20%20x%20%3D%202%0A%20%20%20%20print%28x%29%0A%20%20%20%20h%28%29%0A%0Adef%20h%28%29%3A%0A%20%20%20%20x%20%3D%203%0A%20%20%20%20print%28x%29%0A%0Af%28%29&cumulative=false&curInstr=0&heapPrimitives=nevernest&mode=display&origin=opt-frontend.js&py=3&rawInputLstJSON=%5B%5D&textReferences=false)..." ] }, { @@ -149,7 +137,7 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "Then let's make some changes to it! [link](https://pythontutor.com/visualize.html#code=x%20%3D%20100%0A%0Adef%20f%28%29%3A%0A%20%20%20%20x%20%3D%201%0A%20%20%20%20print%28x%29%0A%20%20%20%20g%28%29%0A%0Adef%20g%28%29%3A%0A%20%20%20%20x%20%3D%202%0A%20%20%20%20print%28x%29%0A%20%20%20%20h%28%29%0A%0Adef%20h%28%29%3A%0A%20%20%20%20print%28x%29%0A%0Af%28%29&cumulative=false&curInstr=0&heapPrimitives=nevernest&mode=display&origin=opt-frontend.js&py=3&rawInputLstJSON=%5B%5D&textReferences=false)" + "Warmup 4 -- Let's make some changes to it! [link](https://pythontutor.com/visualize.html#code=x%20%3D%20100%0A%0Adef%20f%28%29%3A%0A%20%20%20%20x%20%3D%201%0A%20%20%20%20print%28x%29%0A%20%20%20%20g%28%29%0A%0Adef%20g%28%29%3A%0A%20%20%20%20x%20%3D%202%0A%20%20%20%20print%28x%29%0A%20%20%20%20h%28%29%0A%0Adef%20h%28%29%3A%0A%20%20%20%20print%28x%29%0A%0Af%28%29&cumulative=false&curInstr=0&heapPrimitives=nevernest&mode=display&origin=opt-frontend.js&py=3&rawInputLstJSON=%5B%5D&textReferences=false)" ] }, { @@ -190,7 +178,7 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "And finally [link](https://pythontutor.com/visualize.html#code=x%20%3D%20100%0A%0Adef%20f%28%29%3A%0A%20%20%20%20x%20%3D%201%0A%20%20%20%20print%28x%29%0A%20%20%20%20g%28%29%0A%0Adef%20g%28%29%3A%0A%20%20%20%20global%20x%0A%20%20%20%20x%20%3D%202%0A%20%20%20%20print%28x%29%0A%20%20%20%20h%28%29%0A%0Adef%20h%28%29%3A%0A%20%20%20%20print%28x%29%0A%0Af%28%29&cumulative=false&curInstr=0&heapPrimitives=nevernest&mode=display&origin=opt-frontend.js&py=3&rawInputLstJSON=%5B%5D&textReferences=false)..." + "Warmup 5 -- finally [link](https://pythontutor.com/visualize.html#code=x%20%3D%20100%0A%0Adef%20f%28%29%3A%0A%20%20%20%20x%20%3D%201%0A%20%20%20%20print%28x%29%0A%20%20%20%20g%28%29%0A%0Adef%20g%28%29%3A%0A%20%20%20%20global%20x%0A%20%20%20%20x%20%3D%202%0A%20%20%20%20print%28x%29%0A%20%20%20%20h%28%29%0A%0Adef%20h%28%29%3A%0A%20%20%20%20print%28x%29%0A%0Af%28%29&cumulative=false&curInstr=0&heapPrimitives=nevernest&mode=display&origin=opt-frontend.js&py=3&rawInputLstJSON=%5B%5D&textReferences=false)..." ] }, { @@ -232,7 +220,52 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "## Today: Conditional Statements\n", + "## Reminder\n", + "\n", + "Python has two scopes -- global and local. The callstack stores variables in frames. When executing a line of code, the variables in the current local scope and global scope are accessible.\n", + "\n", + "Some Lessons on executing Functions:\n", + "\n", + "1. Functions don't execute unless they are called\n", + "2. Variables created in a function die after function returns\n", + "3. Variables start fresh every time a function is called again\n", + "4. You can't see the variables of other function invocations, even those that call you\n", + "5. You can generally just use global variables inside a function\n", + "6. If you do an assignment to a variable in a function, Python assumes you want it local\n", + "7. Assignment to a variable should be before its use in a function, even if there's a a global variable with the same name\n", + "8. Use a global declaration to prevent Python from creating a local variable when you want a global variable\n", + "9. In Python, arguments are \"passed by value\", meaning reassignments to a parameter don't change the argument outside\n", + "10. It's irrelevant whether the argument (outside) and parameter (inside) have the same variable name\n" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "# Conditionals 1\n", + "\n", + "## Reading\n", + "\n", + "* [Downey Ch 5 (\"Floor Division and Modulus\" to \"Nested Conditionals\" and \"Keyboard Input\" to end)](https://greenteapress.com/thinkpython2/html/thinkpython2006.html)\n", + "* [Downey Ch 6 (\"Return Values\" to \"Boolean Functions\")](https://greenteapress.com/thinkpython2/html/thinkpython2007.html)\n", + "* [Python for Everybody, 4.1 - 4.5](https://runestone.academy/ns/books/published//py4e-int/conditional/toctree.html)\n", + "\n", + "## Learning Objectives\n", + "After this lecture you will be able to...\n", + " - write conditional statements\n", + " - using conditional execution ( if )\n", + " - using alternate execution (if/else)\n", + " - using chained conditionals (if/elif/elif/…/else)\n", + " - identify code blocks (indentation layers) in a program\n", + " - count the number of blocks in a segment of code\n", + " - determine the output of code with conditional statements" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## if statement Syntax:\n", "\n", "An if statement has the form:\n", "```python\n", @@ -241,7 +274,9 @@ "```\n", "\n", "The `condition` portion is an expression that evaluates to `True` or `False`. If the condition evaluates to True\n", - "then the indented block of code is executed. If it is false then the indented block of code is skipped." + "then the indented block of code is executed. If it is false then the indented block of code is skipped.\n", + "\n", + "### Example usage" ] }, { @@ -274,7 +309,43 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "Bob runs a community bakery. Every day, he recieves a shipment of dough balls, each of which bakes 4 loaves of bread, and he evenly splits the bread among his customers. Some days he may recieve 10 customers, 20 customers, and some days none at all! Below is the code that tells Bob how much bread he gave to each customer.\n", + "### You Try\n", + "In the cell below print \"even\" if the inputed value is even" + ] + }, + { + "cell_type": "code", + "execution_count": 7, + "metadata": {}, + "outputs": [ + { + "name": "stdin", + "output_type": "stream", + "text": [ + "Enter an integer number 4\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "even\n" + ] + } + ], + "source": [ + "num = int(input(\"Enter an integer number\"))\n", + "\n", + "##TODO write an if statement that prints \"even\" if num is holding an even value\n", + "if num%2==0:\n", + " print(\"even\")" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## if-else statement Syntax\n", "\n", "An if-else statement has the form:\n", "\n", @@ -285,19 +356,27 @@ " # block of code to run if condition is false\n", "```\n", "\n", - "The `condition` portion is an expression that evaluates to `True` or `False`. If the condition evaluates to True then the first indented block of code is executed. If it is false then the indented block of code after the `else:` is executed." + "The `condition` portion is an expression that evaluates to `True` or `False`. If the condition evaluates to True then the first indented block of code is executed. If it is false then the indented block of code after the `else:` is executed.\n", + "\n", + "### Example usage\n", + "\n", + "Bob runs a community bakery. Every day, he recieves a shipment of dough balls, each of which bakes 4 loaves of bread, and he evenly splits the bread among his customers. Some days he may recieve 10 customers, some days 20 customers, and some days none at all!\n", + "\n", + "Below is the code that tells Bob how much bread he gave to each customer." ] }, { "cell_type": "code", - "execution_count": 7, - "metadata": {}, + "execution_count": 8, + "metadata": { + "scrolled": true + }, "outputs": [ { "name": "stdin", "output_type": "stream", "text": [ - "How many balls of dough did we recieve? 4\n", + "How many balls of dough did we recieve? 3\n", "How many customers were there? 8\n" ] }, @@ -305,7 +384,7 @@ "name": "stdout", "output_type": "stream", "text": [ - "Each customer gets 2.0 loaves of bread.\n" + "Each customer gets 1.5 loaves of bread.\n" ] } ], @@ -326,17 +405,49 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "## Age Categorizer\n", - "Assume `age` is an int parameter of `categorize_age`. Categorize the person's age.\n", + "### You Try\n", "\n", - "All the following bounds are inclusive (meaning you should include both ends)\n", + "In the cell below write an if-else statement that prints \"you may enter\" or \"entry denied\" depending\n", + "on the value in the `password` variable. The correct value for entry is **CatNipFOREVER**." + ] + }, + { + "cell_type": "code", + "execution_count": 9, + "metadata": {}, + "outputs": [ + { + "name": "stdin", + "output_type": "stream", + "text": [ + "Enter Password: ········\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "entry denied\n" + ] + } + ], + "source": [ + "from getpass import getpass\n", + "password = getpass(\"Enter Password:\")\n", "\n", - " - Return \"Baby\" if between ages of 0 and 1\n", - " - Return \"Toddler\" if between ages of 2 and 4\n", - " - Return \"Child\" if between ages of 5 and 17\n", - " - Return \"Adult\" if between ages of 18 and 64\n", - " - Return \"Senior\" if between ages of 65 and 125\n", - " - Return \"Not a valid age\" for all other ages.\n", + "##TODO write an if-else statement that prints \"you may enter\" or \"entry denied\" if they entered\n", + "# the proper password (i.e. if password refers to the value \"CatNipFOREVER\")\n", + "if password==\"CatNipFOREVER\":\n", + " print(\"you may enter\")\n", + "else:\n", + " print(\"entry denied\")" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## if-elif statement syntax\n", "\n", "An if-elif statment has the form:\n", "\n", @@ -356,12 +467,28 @@ "\n", "If `condition 1` evaluates to `True` then code block 1 is executed. If it is false then `condition2` is evaluated. If it is `True` then code block 2 is executed. If it is false then `condition3` is evaluated. If it is true then code block 3 is executed. This process continues until the final condition (`conditionN`). If it is true then code block N is executed. If it is false then code block N+1 is executed (if present).\n", "\n", - "Notice that **only one** code block will be executed and if the final else portion is included then **exactly one** code block will be executed." + "Notice that **only one** code block will be executed and if the final else portion is included then **exactly one** code block will be executed.\n", + "\n", + "### Example Usage\n", + "\n", + "**Age Categorizer**\n", + "\n", + "Assume `age` is an int parameter of the `categorize_age()` function, which categorizes the person's age.\n", + "\n", + "All the following bounds are inclusive (meaning you should include both ends)\n", + "\n", + " - Return \"Baby\" if between ages of 0 and 1\n", + " - Return \"Toddler\" if between ages of 2 and 4\n", + " - Return \"Child\" if between ages of 5 and 17\n", + " - Return \"Adult\" if between ages of 18 and 64\n", + " - Return \"Senior\" if between ages of 65 and 125\n", + " - Return \"Not a valid age\" for all other ages.\n", + "\n" ] }, { "cell_type": "code", - "execution_count": 8, + "execution_count": 12, "metadata": {}, "outputs": [ { @@ -416,7 +543,65 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "## Date Printer\n", + "### You Try\n", + "\n", + "In the cell below finish writing the `calculate_letter_grade()` function. The input to the function is a percentage (float) and the return value should be a letter grade following these rules:\n", + "\n", + "- \\>= 93 - A\n", + "- \\>= 88 - AB\n", + "- \\>= 80 - B\n", + "- \\>= 75 - BC\n", + "- \\>= 70 - C\n", + "- \\>= 60 - D\n", + "- anything else - F" + ] + }, + { + "cell_type": "code", + "execution_count": 15, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "B\n" + ] + } + ], + "source": [ + "##TODO finish the calculate_letter_grade() function\n", + "def calculate_letter_grade(percentage):\n", + " \"\"\"returns the letter grade for the provided course percentage\"\"\"\n", + " if percentage >= 93:\n", + " return \"A\"\n", + " elif percentage >= 88:\n", + " return \"AB\"\n", + " elif percentage >= 80:\n", + " return \"B\"\n", + " elif percentage >= 75:\n", + " return \"BC\"\n", + " elif percentage >= 70:\n", + " return \"C\"\n", + " elif percentage >= 60:\n", + " return \"D\"\n", + " else:\n", + " return \"F\"\n", + "\n", + "# Should work with the following function call. Try different values\n", + "print(calculate_letter_grade(86.5))" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Practice\n", + "\n", + "Let's work through computing a string representation of a date given the year, month, and day.\n", + "\n", + "\n", + "### Date Printer\n", "\n", "See the requirements below. We will first break down the problem into smaller problems and right a function for each." ] @@ -435,7 +620,7 @@ }, { "cell_type": "code", - "execution_count": 9, + "execution_count": 16, "metadata": {}, "outputs": [ { @@ -489,7 +674,7 @@ }, { "cell_type": "code", - "execution_count": 10, + "execution_count": 17, "metadata": {}, "outputs": [ { @@ -531,7 +716,7 @@ }, { "cell_type": "code", - "execution_count": 11, + "execution_count": 18, "metadata": {}, "outputs": [ { @@ -573,7 +758,7 @@ }, { "cell_type": "code", - "execution_count": 12, + "execution_count": 19, "metadata": {}, "outputs": [ { @@ -602,6 +787,20 @@ "print(format_date(1997, 10, 22))\n", "print(format_date(1497, 6, 8))" ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Summary\n", + "\n", + "You have learned about the syntax for the three types of conditional statements and you have practiced writing code using each of them.\n", + "\n", + "- **if** statement\n", + "- **if-else** statement\n", + "- **if-elif** statement\n", + "\n" + ] } ], "metadata": { @@ -620,7 +819,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.11.5" + "version": "3.10.12" } }, "nbformat": 4,