diff --git a/f22/andy_lec_notes/lec_09/.ipynb_checkpoints/lec09_conditionals2_template-checkpoint.ipynb b/f22/andy_lec_notes/lec09_Sep26_Conditionals2/.ipynb_checkpoints/lec09_conditionals2_template-checkpoint.ipynb similarity index 100% rename from f22/andy_lec_notes/lec_09/.ipynb_checkpoints/lec09_conditionals2_template-checkpoint.ipynb rename to f22/andy_lec_notes/lec09_Sep26_Conditionals2/.ipynb_checkpoints/lec09_conditionals2_template-checkpoint.ipynb diff --git a/f22/andy_lec_notes/lec_09/lec09_conditionals2_850.ipynb b/f22/andy_lec_notes/lec09_Sep26_Conditionals2/lec09_conditionals2_completed.ipynb similarity index 94% rename from f22/andy_lec_notes/lec_09/lec09_conditionals2_850.ipynb rename to f22/andy_lec_notes/lec09_Sep26_Conditionals2/lec09_conditionals2_completed.ipynb index af23921b553de50fb94b4fc7cfd6f7b95b3eb614..35b84ac6944c130eaaa87de236d631d8a1817615 100644 --- a/f22/andy_lec_notes/lec_09/lec09_conditionals2_850.ipynb +++ b/f22/andy_lec_notes/lec09_Sep26_Conditionals2/lec09_conditionals2_completed.ipynb @@ -2,7 +2,7 @@ "cells": [ { "cell_type": "code", - "execution_count": 2, + "execution_count": 5, "metadata": {}, "outputs": [ { @@ -20,11 +20,9 @@ "\n", "\n", "# What is the Python interpreter actually doing? ....\n", - "\n", - "# if the first part of an 'or' is false, Python just gives us the 2nd part\n", + "# if the first part of an 'or' is false, ... \n", "\n", "team = \"Badgers\"\n", - "\n", "print(team == \"Gophers\" or \"Hawkeyes\" or \"Bulldogs\")\n", "\n", "if team == \"Gophers\" or \"Hawkeyes\" or \"Spartans\": \n", @@ -39,7 +37,7 @@ }, { "cell_type": "code", - "execution_count": 5, + "execution_count": 8, "metadata": {}, "outputs": [ { @@ -53,30 +51,29 @@ "source": [ "# Warmup 2: Copy/paste the code above and fix the if statement\n", "\n", - "team = \"Spartans\"\n", + "team = \"Hawkeyes\"\n", "\n", - "if team == \"Gophers\" or team == \"Hawkeyes\" or team == \"Spartans\": \n", + "if team == \"Gophers\" or team == \"Hawkeyes\" or team ==\"Spartans\": \n", " print (\"boo!!\")\n", "else:\n", " print(\"yay!!\")\n", "\n", "\n", "# test it with several different values of phrase\n", - "\n", - "# \n" + "\n" ] }, { "cell_type": "code", - "execution_count": 12, + "execution_count": 9, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ - "Feb\n", - "May\n" + "N/A\n", + "\n" ] } ], @@ -112,13 +109,13 @@ " else:\n", " return \"N/A\"\n", "\n", - "print(month_to_str(2))\n", - "print(month_to_str(5))" + "print(month_to_str(\"January\"))\n", + "print()" ] }, { "cell_type": "code", - "execution_count": 13, + "execution_count": 32, "metadata": {}, "outputs": [ { @@ -146,12 +143,12 @@ " return str(day) + \"rd\"\n", " elif day == 2 or day == 22:\n", " return str(day) + \"nd\"\n", - " elif 4 <= day <= 20 or 24 <= day <= 30:\n", + " elif 4 <= day <= 30: # TODO: complete this\n", " return str(day) + \"th\"\n", + " # TODO: add an else\n", " else:\n", " return \"N/A\"\n", " \n", - " \n", "print(day_to_str(21))\n", "print(day_to_str(6))\n", "print(day_to_str(11))\n", @@ -170,7 +167,7 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "# February 14: Conditionals 2\n", + "# Lecture 9: Conditionals 2\n", "Learning Objectives\n", "\n", "- Read and Write Nested conditional statements\n", @@ -192,73 +189,33 @@ "### Read and Write Nested Conditional Statements" ] }, - { - "cell_type": "code", - "execution_count": 12, - "metadata": {}, - "outputs": [], - "source": [ - "# Look at the slides" - ] - }, - { - "cell_type": "code", - "execution_count": 10, - "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "duct tape\n", - "WD-40\n", - "good\n" - ] - } - ], - "source": [ - "# Nesting Example 1: Fixing it\n", - "\n", - "def fix(moves, should):\n", - " if moves:\n", - " if should:\n", - " return \"good\"\n", - " else:\n", - " return \"duct tape\"\n", - " else:\n", - " if should:\n", - " return \"WD-40\"\n", - " else:\n", - " return \"good\"\n", - " \n", - "print(fix(moves=True, should=False))\n", - "print(fix(moves=False, should=True))\n", - "print(fix(moves=False, should=False))" - ] - }, { "cell_type": "code", "execution_count": 14, "metadata": {}, "outputs": [], "source": [ - "# Nesting Example 2: stoplight" + "# Nesting Example: stoplight\n", + "\n", + "# Imagine what you do in these driving situations: \n", + "# Consider the color of the light \n", + "# as well as your distance from the light" ] }, { "cell_type": "code", - "execution_count": 11, + "execution_count": 18, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ - "continue\n", - "slow to a stop\n", + "go\n", + "stop\n", "coast\n", "speed up\n", - "speed up\n" + "None\n" ] } ], @@ -266,21 +223,19 @@ "def stop_light(color, distance = 100):\n", " \"\"\"given color red/yellow/green and distance in feet, return what to do as a str\"\"\"\n", " if color == \"red\":\n", - " if distance < 50:\n", - " return \"slow to a stop\"\n", + " if distance < 150:\n", + " return \"stop\"\n", " else:\n", " return \"coast\"\n", " elif color == \"yellow\":\n", - " if distance < 100:\n", + " if distance < 50:\n", " return \"speed up\"\n", - " else:\n", - " return \"slow to a stop\"\n", " else:\n", - " return \"continue\"\n", + " return \"go\"\n", " \n", "print(stop_light(\"green\", 20))\n", "print(stop_light(\"red\", 30))\n", - "print(stop_light(\"red\", 100))\n", + "print(stop_light(\"red\", 200))\n", "print(stop_light(\"yellow\", 20))\n", "print(stop_light(\"yellow\", 60))" ] @@ -297,10 +252,37 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 20, "metadata": {}, - "outputs": [], - "source": [] + "outputs": [ + { + "ename": "SyntaxError", + "evalue": "invalid syntax (184380567.py, line 7)", + "output_type": "error", + "traceback": [ + "\u001b[0;36m Input \u001b[0;32mIn [20]\u001b[0;36m\u001b[0m\n\u001b[0;31m elif color == \"yellow\":\u001b[0m\n\u001b[0m ^\u001b[0m\n\u001b[0;31mSyntaxError\u001b[0m\u001b[0;31m:\u001b[0m invalid syntax\n" + ] + } + ], + "source": [ + "def stop_light2(color, distance = 100):\n", + " \"\"\"given color red/yellow/green and distance in feet, return what to do as a str\"\"\"\n", + " if color == \"red\" and distance < 150:\n", + " return \"stop\"\n", + " elif color == \"red\" and distance >= 150:\n", + " return \"coast\"\n", + " elif color == \"yellow\":\n", + " if distance < 50:\n", + " return \"speed up\"\n", + " else:\n", + " return \"go\"\n", + " \n", + "print(stop_light2(\"green\", 20))\n", + "print(stop_light2(\"red\", 30))\n", + "print(stop_light2(\"red\", 200))\n", + "print(stop_light2(\"yellow\", 20))\n", + "print(stop_light2(\"yellow\", 60))" + ] }, { "cell_type": "code", @@ -308,7 +290,7 @@ "metadata": {}, "outputs": [], "source": [ - "# Nesting Example 3: Date Printer" + "# Nesting Example : Date Printer" ] }, { @@ -336,7 +318,7 @@ }, { "cell_type": "code", - "execution_count": 16, + "execution_count": 24, "metadata": {}, "outputs": [ { @@ -356,13 +338,12 @@ " \"\"\"Convert a year (as an integer) into a string. If the year is < 2000, return the full 4 digits\n", " Otherwise, return the last 2 digits with a single quote in front\"\"\"\n", " if year >= 2000:\n", - " if year < 2010:\n", - " return \"'0\" + str(year % 100)\n", - " else:\n", - " return \"'\" + str(year % 100)\n", + " last_two = year % 100\n", + " if last_two < 10:\n", + " return \"\\'0\" + str(last_two)\n", + " return \"\\'\" + str(last_two)\n", " else:\n", " return str(year)\n", - "\n", " \n", "print(year_to_str(1998))\n", "print(year_to_str(2013))\n", @@ -380,15 +361,14 @@ }, { "cell_type": "code", - "execution_count": 18, + "execution_count": 25, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ - "the date is: Feb 14th of '22\n", - "the date is: Feb 12th of '06\n" + "the date is: Feb 14th of '22\n" ] } ], @@ -397,13 +377,12 @@ "def date_printer(month=1, day=1, year=1970):\n", " print(\"the date is: \", month_to_str(month), day_to_str(day), \"of\", year_to_str(year))\n", " \n", - "date_printer(2, 14, 2022)\n", - "date_printer(2, 12, 2006)" + "date_printer(2, 14, 2022)" ] }, { "cell_type": "code", - "execution_count": 19, + "execution_count": 26, "metadata": {}, "outputs": [ { @@ -420,7 +399,7 @@ }, { "cell_type": "code", - "execution_count": 20, + "execution_count": 27, "metadata": {}, "outputs": [ { @@ -437,14 +416,24 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 33, "metadata": {}, - "outputs": [], - "source": [] + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "the date is: Sep 26th of '23\n" + ] + } + ], + "source": [ + "date_printer(9, 26, 2023)" + ] }, { "cell_type": "code", - "execution_count": 1, + "execution_count": null, "metadata": {}, "outputs": [], "source": [] @@ -487,7 +476,7 @@ }, { "cell_type": "code", - "execution_count": 21, + "execution_count": 34, "metadata": {}, "outputs": [ { @@ -523,7 +512,7 @@ }, { "cell_type": "code", - "execution_count": 22, + "execution_count": 36, "metadata": {}, "outputs": [ { @@ -542,15 +531,16 @@ " if b == 17:\n", " if c == 55:\n", " return True\n", - " else:\n", + " else: # incorrect indentation\n", " return False\n", " \n", - "print(bad_combo(10, 12, 14))" + "print(bad_combo(10, 12, 14))\n", + "\n" ] }, { "cell_type": "code", - "execution_count": 24, + "execution_count": 37, "metadata": {}, "outputs": [ { @@ -614,6 +604,10 @@ "source": [ "# Refactoring Example 2: too much code\n", "def check_different(b1, b2):\n", + " ''' \n", + " returns True if booleans b1 and b2 are different\n", + " and False otherwise\n", + " '''\n", " if b1 == True and b2 == False:\n", " return True\n", " elif b1 == False and b2 == True:\n", @@ -630,7 +624,7 @@ }, { "cell_type": "code", - "execution_count": 26, + "execution_count": 40, "metadata": {}, "outputs": [ { @@ -639,18 +633,8 @@ "text": [ "True\n", "False\n", - "True\n" - ] - }, - { - "ename": "NameError", - "evalue": "name 'refator_check_different' is not defined", - "output_type": "error", - "traceback": [ - "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", - "\u001b[0;31mNameError\u001b[0m Traceback (most recent call last)", - "\u001b[0;32m<ipython-input-26-80d33f32e531>\u001b[0m in \u001b[0;36m<module>\u001b[0;34m\u001b[0m\n\u001b[1;32m 4\u001b[0m \u001b[0mprint\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mrefactor_check_different\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;32mFalse\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;32mFalse\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 5\u001b[0m \u001b[0mprint\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mrefactor_check_different\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;32mFalse\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;32mTrue\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m----> 6\u001b[0;31m \u001b[0mprint\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mrefator_check_different\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;32mTrue\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;32mTrue\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m", - "\u001b[0;31mNameError\u001b[0m: name 'refator_check_different' is not defined" + "True\n", + "False\n" ] } ], @@ -660,16 +644,7 @@ "print(refactor_check_different(True, False))\n", "print(refactor_check_different(False, False))\n", "print(refactor_check_different(False, True))\n", - "print(refator_check_different(True, True))" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "# if time...refactor another previous example from this lecture" + "print(refactor_check_different(True, True))" ] }, { @@ -707,11 +682,13 @@ "### After Lecture Practice:\n", "\n", "\n", - "1. Read the slides ....while you do the Interactive Exercises.\n", + "1. Try the Interactive Exercises:\n", + "\n", + "https://cs220.cs.wisc.edu/f22/materials/lec-09.html\n", "\n", "2. Talk through them with a partner.\n", "\n", - "3. Try these three sample Exam problems\n", + "3. Try these three sample **Exam/Quiz problems**\n", "\n" ] }, @@ -777,7 +754,7 @@ "# Consider the following code\n", "\n", "def refactor(x,y):\n", - " if x: # the expression after an if must be a boolean expression\n", + " if x: # assume x is a Boolean expression\n", " return True\n", " elif y:\n", " return True\n", @@ -800,7 +777,7 @@ ], "metadata": { "kernelspec": { - "display_name": "Python 3", + "display_name": "Python 3 (ipykernel)", "language": "python", "name": "python3" }, @@ -814,7 +791,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.9.1" + "version": "3.9.12" } }, "nbformat": 4, diff --git a/f22/andy_lec_notes/lec_09/lec09_conditionals2_template.ipynb b/f22/andy_lec_notes/lec09_Sep26_Conditionals2/lec09_conditionals2_template.ipynb similarity index 98% rename from f22/andy_lec_notes/lec_09/lec09_conditionals2_template.ipynb rename to f22/andy_lec_notes/lec09_Sep26_Conditionals2/lec09_conditionals2_template.ipynb index 07f4e1ad05012b619ebe85e874ce756322b04697..60d8729706b89cde53b8267600f6cf4cc4614597 100644 --- a/f22/andy_lec_notes/lec_09/lec09_conditionals2_template.ipynb +++ b/f22/andy_lec_notes/lec09_Sep26_Conditionals2/lec09_conditionals2_template.ipynb @@ -2,9 +2,17 @@ "cells": [ { "cell_type": "code", - "execution_count": null, + "execution_count": 10, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "boo!!\n" + ] + } + ], "source": [ "# Warmup 1: What's wrong with this code? \n", "# Write your answer here: ....\n", @@ -122,7 +130,7 @@ " return str(day) + \"nd\"\n", " \n", " elif 4 <= day <= 20: # TODO: complete this\n", - " return str(day) + \"th\"\n", + " return \"fix this\"\n", "\n", " # TODO: add an else\n", " \n", @@ -145,7 +153,7 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "# February 14: Conditionals 2\n", + "# Lecture 9: Conditionals 2\n", "Learning Objectives\n", "\n", "- Read and Write Nested conditional statements\n", @@ -168,46 +176,9 @@ ] }, { - "cell_type": "code", - "execution_count": 12, - "metadata": {}, - "outputs": [], - "source": [ - "# Look at the slides" - ] - }, - { - "cell_type": "code", - "execution_count": 4, + "cell_type": "markdown", "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "duct tape\n", - "WD-40\n" - ] - } - ], - "source": [ - "# Nesting Example 1: Fixing it\n", - "\n", - "def fix(moves, should):\n", - " if moves:\n", - " if should:\n", - " return \"good\"\n", - " else:\n", - " return \"duct tape\"\n", - " else:\n", - " if should:\n", - " return \"WD-40\"\n", - " else:\n", - " return \"good\"\n", - " \n", - "print(fix(moves=True, should=False))\n", - "print(fix(moves=False, should=True))" - ] + "source": [] }, { "cell_type": "code", @@ -215,7 +186,11 @@ "metadata": {}, "outputs": [], "source": [ - "# Nesting Example 2: stoplight" + "# Nesting Example: stoplight\n", + "\n", + "# Imagine what you do in these driving situations: \n", + "# Consider the color of the light \n", + "# as well as your distance from the light" ] }, { @@ -271,7 +246,7 @@ "metadata": {}, "outputs": [], "source": [ - "# Nesting Example 3: Date Printer" + "# Nesting Example : Date Printer" ] }, { @@ -554,6 +529,10 @@ "source": [ "# Refactoring Example 2: too much code\n", "def check_different(b1, b2):\n", + " ''' \n", + " returns True if booleans b1 and b2 are different\n", + " and False otherwise\n", + " '''\n", " if b1 == True and b2 == False:\n", " return True\n", " elif b1 == False and b2 == True:\n", @@ -642,7 +621,7 @@ "\n", "2. Talk through them with a partner.\n", "\n", - "3. Try these three sample Exam problems\n", + "3. Try these three sample **Exam/Quiz problems**\n", "\n" ] }, @@ -708,7 +687,7 @@ "# Consider the following code\n", "\n", "def refactor(x,y):\n", - " if x: # the expression after an if must be a boolean expression\n", + " if x: # assume x is a Boolean expression\n", " return True\n", " elif y:\n", " return True\n", @@ -731,7 +710,7 @@ ], "metadata": { "kernelspec": { - "display_name": "Python 3", + "display_name": "Python 3 (ipykernel)", "language": "python", "name": "python3" }, @@ -745,7 +724,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.9.1" + "version": "3.9.12" } }, "nbformat": 4, diff --git a/f22/andy_lec_notes/lec_09/readme.md b/f22/andy_lec_notes/lec09_Sep26_Conditionals2/readme.md similarity index 100% rename from f22/andy_lec_notes/lec_09/readme.md rename to f22/andy_lec_notes/lec09_Sep26_Conditionals2/readme.md