{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "fb312eb6",
   "metadata": {
    "slideshow": {
     "slide_type": "slide"
    }
   },
   "source": [
    "# String Comparison, Chained Conditionals, and Logical Operators\n",
    "#### CS 65: Introduction to Computer Science I"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "e6535225",
   "metadata": {
    "slideshow": {
     "slide_type": "slide"
    }
   },
   "source": [
    "## Conditionals Review\n",
    "\n",
    "So far, we've seen _conditional statements_"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "da7d98e0",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "What is your age? 67\n",
      "Applying 10% senior discount\n",
      "Your total bill is 32.067\n"
     ]
    }
   ],
   "source": [
    "total_bill = 35.63\n",
    "age = float(input(\"What is your age? \"))\n",
    "\n",
    "if age > 65:\n",
    "    print(\"Applying 10% senior discount\")\n",
    "    total_bill = total_bill * 0.9\n",
    "    #any indented code is only runs\n",
    "    #when the condition is True\n",
    "    \n",
    "#un-indented code runs no matter what\n",
    "print(\"Your total bill is\", total_bill)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "f5c3fbd6",
   "metadata": {},
   "source": [
    "conditional statements with two alternatives"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "32936d95",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Enter your height in inches: 61\n",
      "You are tall enough for this ride.\n"
     ]
    }
   ],
   "source": [
    "height = int(input(\"Enter your height in inches: \"))\n",
    "\n",
    "if height < 60:\n",
    "    print(\"You are not tall enough for this ride.\")\n",
    "else:\n",
    "    print(\"You are tall enough for this ride.\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "624b5abf",
   "metadata": {},
   "source": [
    "and in the lab, we saw _nested_ conditional statements"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "72772ad6",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Enter a number: 9\n",
      "9 is odd and divisble by 3\n"
     ]
    }
   ],
   "source": [
    "num = int(input(\"Enter a number: \"))\n",
    "\n",
    "#this code checks if a number is divisible by both 2 and 3\n",
    "if num % 2 == 0:\n",
    "    print(num,\"is even\")\n",
    "else:\n",
    "    if num % 3 == 0:\n",
    "        print(num,\"is odd and divisble by 3\")\n",
    "    else:\n",
    "        print(num,\"is odd and not divisible by 3\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "89a5826a",
   "metadata": {
    "slideshow": {
     "slide_type": "slide"
    }
   },
   "source": [
    "## Group Exercise: Comparing Strings\n",
    "\n",
    "Type the following program into Thonny, and run it several times. What does it do?"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "c0a7abcb",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Enter a name: Eric\n",
      "Enter another name: Manley\n",
      "Eric is first\n"
     ]
    }
   ],
   "source": [
    "name1 = input(\"Enter a name: \")\n",
    "name2 = input(\"Enter another name: \")\n",
    "\n",
    "if name1 < name2:\n",
    "    print(name1,\"is first\")\n",
    "else:\n",
    "    print(name2,\"is first\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "0fecf3dd",
   "metadata": {},
   "source": [
    "Is string comparison in Python case-sensitive? E.g., is `\"Eric\"` the same as `\"eric\"`? How could we test it?"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "38681114",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "markdown",
   "id": "f1069ee6",
   "metadata": {
    "slideshow": {
     "slide_type": "slide"
    }
   },
   "source": [
    "## Character representation\n",
    "\n",
    "Every text character is represented by a numerical code in your computer's memory. \n",
    "\n",
    "They came up with in the 1960's as part of the ASCII standard (https://en.wikipedia.org/wiki/ASCII)\n",
    "\n",
    "Here's some examples\n",
    "\n",
    "<table>\n",
    "<tr>\n",
    "    <td style=\"padding:30px\">\n",
    "        <table>\n",
    "            <tr><th> code </th> <th>character </tr>\n",
    "            <tr><td>33 </td><td> !</td></tr>\n",
    "            <tr><td>34 </td><td> \"</td></tr>\n",
    "            <tr><td>35 </td><td> #</td></tr>\n",
    "            <tr><td>36 </td><td> $</td></tr>\n",
    "            <tr><td>37 </td><td> % </td></tr>      \n",
    "        </table>      \n",
    "    </td>\n",
    "    <td style=\"padding:30px\">\n",
    "        <table>\n",
    "            <tr><th> code </th> <th>character </tr>\n",
    "            <tr><td>65 </td><td> A</td></tr>\n",
    "            <tr><td>66 </td><td> B</td></tr>\n",
    "            <tr><td>67 </td><td> C</td></tr>\n",
    "            <tr><td>68 </td><td> D</td></tr>\n",
    "            <tr><td>69 </td><td> E </td></tr>      \n",
    "        </table>\n",
    "    </td>\n",
    "    <td style=\"padding:30px\">\n",
    "        <table>\n",
    "            <tr><th> code </th> <th>character </tr>\n",
    "            <tr><td>97 </td><td> a</td></tr>\n",
    "            <tr><td>98 </td><td> b</td></tr>\n",
    "            <tr><td>99 </td><td> c</td></tr>\n",
    "            <tr><td>100 </td><td> d</td></tr>\n",
    "            <tr><td>101 </td><td> e </td></tr>      \n",
    "        </table>     \n",
    "    </td\n",
    "</tr>\n",
    "</table>\n",
    "    \n",
    "You can use the built-in `ord()` function to look up the code for any given character. Use `chr()` to do the opposite."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "id": "a28db44b",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "65"
      ]
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "ord(\"A\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "id": "f6a45e70",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'A'"
      ]
     },
     "execution_count": 14,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "chr(65)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "b8463c9e",
   "metadata": {
    "slideshow": {
     "slide_type": "slide"
    }
   },
   "source": [
    "## Other conditional statement options\n",
    "\n",
    "There are also __chained conditional statements__ when you have more than two choices\n",
    "\n",
    "`elif` a new keyword meaning \"else if\" which allows you to add as many more options to a conditional statement as you want."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "1636b35c",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "What's the temperature? 40\n",
      "Let's put on jackets and go hiking.\n"
     ]
    }
   ],
   "source": [
    "temp = float(input(\"What's the temperature? \"))\n",
    "\n",
    "if temp >= 70:\n",
    "    print(\"Let's go hiking.\")\n",
    "elif temp >= 30:\n",
    "    print(\"Let's put on jackets and go hiking.\")\n",
    "elif temp >= 10:\n",
    "    print(\"Let's go skiing.\")\n",
    "else:\n",
    "    print(\"It's too cold. Let's stay in and code.\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "b2b0c20b",
   "metadata": {},
   "source": [
    "Note:\n",
    "* `else` part is still optional but gives you the opportunity for \"default\" behavior\n",
    "* at most one block in a `if-elif-else statement` will run"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "39991c37",
   "metadata": {
    "slideshow": {
     "slide_type": "subslide"
    }
   },
   "source": [
    "## a common chained-conditional logic error"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "28065030",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "What's the temperature? 80\n",
      "Let's go skiing.\n"
     ]
    }
   ],
   "source": [
    "temp = float(input(\"What's the temperature? \"))\n",
    "\n",
    "if temp >= 10:\n",
    "    print(\"Let's go skiing.\")\n",
    "elif temp >= 30:\n",
    "    print(\"Let's put on jackets and go hiking.\")\n",
    "elif temp >= 70:\n",
    "    print(\"Let's go hiking.\")\n",
    "else:\n",
    "    print(\"It's too cold. Let's stay in and code.\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "1b94ca3a",
   "metadata": {},
   "source": [
    "Make sure to put the conditions in the order you want to check them - the first one to evalute to `True` will be the one that runs."
   ]
  },
  {
   "cell_type": "markdown",
   "id": "87b464e7",
   "metadata": {
    "slideshow": {
     "slide_type": "slide"
    }
   },
   "source": [
    "## Logical Operators\n",
    "\n",
    "__Logical operators__ are operators that take Booleans as operands: `and`, `or`, `not`"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "0479f27a",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "What's the temperature? 60\n",
      "Is it raining? yes\n",
      "Grab your coat.\n"
     ]
    }
   ],
   "source": [
    "temp = float(input(\"What's the temperature? \"))\n",
    "raining = input(\"Is it raining? \")\n",
    "\n",
    "if temp < 50 or raining == 'yes':\n",
    "    print(\"Grab your coat.\")\n",
    "\n",
    "if not (raining == 'yes') and temp > 70:\n",
    "    print(\"Grab your mitt.\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "9268fcdd",
   "metadata": {
    "slideshow": {
     "slide_type": "slide"
    }
   },
   "outputs": [],
   "source": [
    "a = 2\n",
    "b = 4\n",
    "c = 6"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "6240fbac",
   "metadata": {},
   "source": [
    "See if you can work out the values below before the answer is revealed"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "f43a0a30",
   "metadata": {
    "slideshow": {
     "slide_type": "-"
    }
   },
   "outputs": [],
   "source": [
    "a == 4 or b > 2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "cb0246ae",
   "metadata": {
    "slideshow": {
     "slide_type": "-"
    }
   },
   "outputs": [],
   "source": [
    "6 <= c and a > 3"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "35b6a0cc",
   "metadata": {
    "slideshow": {
     "slide_type": "-"
    }
   },
   "outputs": [],
   "source": [
    "1 != b and c != 3"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "f4e30662",
   "metadata": {
    "slideshow": {
     "slide_type": "-"
    }
   },
   "outputs": [],
   "source": [
    "a >= -1 or a <= b"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "e8fd899b",
   "metadata": {
    "slideshow": {
     "slide_type": "-"
    }
   },
   "outputs": [],
   "source": [
    "not a > 2"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "348bf1bd",
   "metadata": {
    "slideshow": {
     "slide_type": "slide"
    }
   },
   "source": [
    "Logical operators are pretty intuitive because we use them in our natural language, but they can sometimes be tricky when you get complex expressions."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "2e6d6d89",
   "metadata": {},
   "outputs": [],
   "source": [
    "a = 2\n",
    "b = 4\n",
    "c = 6\n",
    "result = a == 2 or b != 2 and c != 6 or a == b + c\n",
    "print(result)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "b8b28570",
   "metadata": {
    "slideshow": {
     "slide_type": "fragment"
    }
   },
   "source": [
    "Rule of thumb: Always use parentheses when you have more than one logical operator"
   ]
  }
 ],
 "metadata": {
  "celltoolbar": "Slideshow",
  "kernelspec": {
   "display_name": "Python 3",
   "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.8.8"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
