{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "144c346e",
   "metadata": {
    "slideshow": {
     "slide_type": "slide"
    }
   },
   "source": [
    "# Conditionals\n",
    "#### CS 65: Introduction to Computer Science I"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "479a938e",
   "metadata": {
    "slideshow": {
     "slide_type": "slide"
    }
   },
   "source": [
    "## Conditional execution of code\n",
    "\n",
    "Sometimes, you want your program to do something different based on different conditions. These are called __conditional statements__.\n",
    "\n",
    "For example, consider the pay calculator. If this employer offers 50% extra pay for overtime (i.e., worked > 40 hours), how could we account for that?"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "41457281",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Enter your hourly wage: 10\n",
      "Enter your number of hours worked: 30\n",
      "Total pay:  250.0\n"
     ]
    }
   ],
   "source": [
    "wage = float(input(\"Enter your hourly wage: \"))\n",
    "hours = float(input(\"Enter your number of hours worked: \"))\n",
    "\n",
    "#option 1: only correct if they didn't work overtime\n",
    "#pay = wage*hours\n",
    "\n",
    "#option 2: only correct if they worked overtime\n",
    "overtime_hours = hours-40\n",
    "pay = (wage*40) + (wage*1.5*overtime_hours)\n",
    "\n",
    "print(\"Total pay: \",pay)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "50e0bd0a",
   "metadata": {
    "slideshow": {
     "slide_type": "slide"
    }
   },
   "source": [
    "## Syntax for `if` statements\n",
    "\n",
    "* keyword `if`\n",
    "* a condition\n",
    "* colon `:`\n",
    "* indented _block_ of code"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "6c442fff",
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "What is your age? 64\n",
      "Your total bill is 35.63\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": "3d23afa9",
   "metadata": {
    "slideshow": {
     "slide_type": "slide"
    }
   },
   "source": [
    "## Syntax for `if-else` statements\n",
    "\n",
    "* keyword `if`\n",
    "* a condition\n",
    "* colon `:`\n",
    "* indented block of code\n",
    "* keyword `else`\n",
    "* colon `:`\n",
    "* indented block of code"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "672e75e5",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Enter your height in inches: 62\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": "4bfc8f4f",
   "metadata": {
    "slideshow": {
     "slide_type": "slide"
    }
   },
   "source": [
    "## Group Exercise\n",
    "\n",
    "How could we make this work using an `if-else`?"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "id": "7ee7bf6e",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Enter your hourly wage: 10\n",
      "Enter your number of hours worked: 40\n",
      "Total pay:  400.0\n"
     ]
    }
   ],
   "source": [
    "wage = float(input(\"Enter your hourly wage: \"))\n",
    "hours = float(input(\"Enter your number of hours worked: \"))\n",
    "\n",
    "if hours <= 40: #should this be < 41\n",
    "    #option 1: only correct if they didn't work overtime\n",
    "    pay = wage*hours\n",
    "else:\n",
    "    #option 2: only correct if they worked overtime\n",
    "    overtime_hours = hours-40\n",
    "    pay = (wage*40) + (wage*1.5*overtime_hours)\n",
    "\n",
    "print(\"Total pay: \",pay)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "51ac472d",
   "metadata": {
    "slideshow": {
     "slide_type": "slide"
    }
   },
   "source": [
    "## Boolean type\n",
    "\n",
    "The __Boolean__ type has two possible values: `True` and `False`\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "id": "54ed1947",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "The condition it True!\n"
     ]
    }
   ],
   "source": [
    "bool_variable = True\n",
    "\n",
    "if bool_variable:\n",
    "    print(\"The condition it True!\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "3667a256",
   "metadata": {},
   "source": [
    "The _condition_ for an `if`/`if-else` statement can be anything that results in a Boolean. An expression, like `height < 60` that results in a Boolean is called a __Boolean expression__."
   ]
  },
  {
   "cell_type": "markdown",
   "id": "dfcbba04",
   "metadata": {
    "slideshow": {
     "slide_type": "slide"
    }
   },
   "source": [
    "## Some Boolean operators\n",
    "\n",
    "* `<` less than\n",
    "* `>` greater than\n",
    "* `<=` less than or equal\n",
    "    - note there is no $\\leq$ button on your keyboard\n",
    "* `>=` greater than or equal\n",
    "* `==` equal (comparison)\n",
    "    - note that `=` is used for assignment, they're different operators, don't confuse them!\n",
    "* `!=` not equal"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "id": "d1003773",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Guess a number: 7\n",
      "That was right, good guess!\n"
     ]
    }
   ],
   "source": [
    "secret_number = 7\n",
    "guess = int(input(\"Guess a number: \"))\n",
    "\n",
    "if guess == secret_number:\n",
    "    print(\"That was right, good guess!\")\n",
    "else:\n",
    "    print(\"Wrong!\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "id": "552b381c",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Enter number of people at the table: 10\n",
      "Was this a business lunch for a non-profit organization? no\n",
      "Calculating 6% sales tax.\n",
      "Automatically adding 15% gratuity.\n",
      "Your total is 148.10850000000002\n"
     ]
    }
   ],
   "source": [
    "total_bill = 121.50\n",
    "party_size = int(input(\"Enter number of people at the table: \"))\n",
    "non_profit = input(\"Was this a business lunch for a non-profit organization? \")\n",
    " \n",
    "if non_profit != \"yes\":\n",
    "    print(\"Calculating 6% sales tax.\")\n",
    "    total_bill = total_bill * 1.06\n",
    "    \n",
    "if party_size >= 8:\n",
    "    print(\"Automatically adding 15% gratuity.\")\n",
    "    total_bill = total_bill * 1.15\n",
    "    \n",
    "print(\"Your total is\",total_bill)"
   ]
  }
 ],
 "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
}
