{
  "nbformat": 4,
  "nbformat_minor": 5,
  "metadata": {
    "kernelspec": {
      "display_name": "Python 3",
      "language": "python",
      "name": "python3"
    }
  },
  "cells": [
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": "# ZHUDDLE | Chapter 03: Conditional execution\n\n8 coding missions + 10 MCQs. 100 XP.\n\nAdapted from Charles R. Severance, Python for Everybody (CC BY 4.0).\n\nSource: https://www.py4e.com/html3/03-conditional\n\nReading: https://www.py4e.com/html3/03-conditional\n\nThe browser runs five independent cases per coding mission. This notebook supplies the first sample; input prompts here are interactive.",
      "id": "conditionals-000"
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": "## C01 | Boolean comparisons\n\nCompare supplied `x` and `y`. Save `equal` (==), `different` (!=), `greater` (>), `at_least` (>=), and `at_most` (<=) as booleans.\n\nFive independent checks are worth 2 XP each. The checker supplies the variables below with different values on each check. Use them without replacing their starting values.\n\n```python\nx = 5\ny = 5\n```\n\n**Source checkpoint:** Boolean expressions",
      "id": "conditionals-001"
    },
    {
      "cell_type": "code",
      "metadata": {},
      "source": "x = 5\ny = 5",
      "execution_count": null,
      "outputs": [],
      "id": "conditionals-002"
    },
    {
      "cell_type": "code",
      "metadata": {},
      "source": "equal = None\ndifferent = None\ngreater = None\nat_least = None\nat_most = None",
      "execution_count": null,
      "outputs": [],
      "id": "conditionals-003"
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": "## C02 | Logical combinations\n\nFor supplied integer `n`, save `single_digit` as whether 0 < n < 10 using `and`. Save `divisible` as whether n is divisible by 2 or 3 using `or`. Save `not_positive` using `not` on n > 0.\n\nFive independent checks are worth 2 XP each. The checker supplies the variables below with different values on each check. Use them without replacing their starting values.\n\n```python\nn = 6\n```\n\n**Source checkpoint:** Logical operators",
      "id": "conditionals-004"
    },
    {
      "cell_type": "code",
      "metadata": {},
      "source": "n = 6",
      "execution_count": null,
      "outputs": [],
      "id": "conditionals-005"
    },
    {
      "cell_type": "code",
      "metadata": {},
      "source": "single_digit = None\ndivisible = None\nnot_positive = None",
      "execution_count": null,
      "outputs": [],
      "id": "conditionals-006"
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": "## C03 | Choose even or odd\n\nUse `if` and `else` to set `parity` to \"even\" or \"odd\" for supplied integer `n`. Print parity.\n\nFive independent checks are worth 2 XP each. The checker supplies the variables below with different values on each check. Use them without replacing their starting values.\n\n```python\nn = 4\n```\n\n**Source checkpoint:** Alternative execution",
      "id": "conditionals-007"
    },
    {
      "cell_type": "code",
      "metadata": {},
      "source": "n = 4",
      "execution_count": null,
      "outputs": [],
      "id": "conditionals-008"
    },
    {
      "cell_type": "code",
      "metadata": {},
      "source": "# Choose one of two branches.",
      "execution_count": null,
      "outputs": [],
      "id": "conditionals-009"
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": "## C04 | Three-way comparison\n\nUse an if/elif/else chain to compare `x` and `y`. Set `relation` to \"less\", \"equal\", or \"greater\" and print it.\n\nFive independent checks are worth 2 XP each. The checker supplies the variables below with different values on each check. Use them without replacing their starting values.\n\n```python\nx = 1\ny = 2\n```\n\n**Source checkpoint:** Chained conditionals",
      "id": "conditionals-010"
    },
    {
      "cell_type": "code",
      "metadata": {},
      "source": "x = 1\ny = 2",
      "execution_count": null,
      "outputs": [],
      "id": "conditionals-011"
    },
    {
      "cell_type": "code",
      "metadata": {},
      "source": "# Compare x with y.",
      "execution_count": null,
      "outputs": [],
      "id": "conditionals-012"
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": "## C05 | Guard the division\n\nSet `safe` to the boolean result of x >= 2 and x / y > 2. Add a `y != 0` guard before division so a zero denominator makes the result False instead of raising an error.\n\nFive independent checks are worth 2 XP each. The checker supplies the variables below with different values on each check. Use them without replacing their starting values.\n\n```python\nx = 6\ny = 2\n```\n\n**Source checkpoint:** Short-circuit evaluation of logical expressions",
      "id": "conditionals-013"
    },
    {
      "cell_type": "code",
      "metadata": {},
      "source": "x = 6\ny = 2",
      "execution_count": null,
      "outputs": [],
      "id": "conditionals-014"
    },
    {
      "cell_type": "code",
      "metadata": {},
      "source": "safe = None",
      "execution_count": null,
      "outputs": [],
      "id": "conditionals-015"
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": "## C06 | A safer temperature input\n\nRead Fahrenheit using `input()`. In a `try` block, convert it to float and print (fahrenheit - 32) * 5 / 9. On invalid text, print exactly `Please enter a number`.\n\nFive independent checks are worth 2 XP each. \n\nUse `input()` normally: the checker enters test values for you. Sample inputs, in order: `32`. Input prompts are omitted from captured output.\n\n**Source checkpoint:** Catching exceptions using try and except",
      "id": "conditionals-016"
    },
    {
      "cell_type": "code",
      "metadata": {},
      "source": "# Read input and handle conversion errors.",
      "execution_count": null,
      "outputs": [],
      "id": "conditionals-017"
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": "## C07 | Overtime with error handling\n\nRead hours and rate as floats with `input()`. Pay 1.5 times the rate for hours above 40, and the ordinary rate for other hours. Print only the pay. If either input is not numeric, print `Error, please enter numeric input`. Use conditionals, without defining a function.\n\nFive independent checks are worth 2 XP each. \n\nUse `input()` normally: the checker enters test values for you. Sample inputs, in order: `45`, `10`. Input prompts are omitted from captured output.\n\n**Source checkpoint:** Exercises: Exercises 1 and 2",
      "id": "conditionals-018"
    },
    {
      "cell_type": "code",
      "metadata": {},
      "source": "# Read two values, handle errors, and calculate overtime.",
      "execution_count": null,
      "outputs": [],
      "id": "conditionals-019"
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": "## C08 | Score to letter grade\n\nRead a numeric score with `input()`. For scores from 0 to 1, print A at 0.9+, B at 0.8+, C at 0.7+, D at 0.6+, otherwise F. Print `Bad score` for out-of-range values or nonnumeric text. Each check covers a group of inputs, including boundaries.\n\nFive independent checks are worth 2 XP each. \n\n**Source checkpoint:** Exercises: Exercise 3",
      "id": "conditionals-020"
    },
    {
      "cell_type": "code",
      "metadata": {},
      "source": "# Validate the score before choosing a grade.",
      "execution_count": null,
      "outputs": [],
      "id": "conditionals-021"
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": "## M01 | Boolean type\n\nWhich is a boolean value?\n\nA. \"True\"\nB. True\nC. \"False\"\nD. \"bool\"",
      "id": "conditionals-022"
    },
    {
      "cell_type": "code",
      "metadata": {},
      "source": "m01 = \"\"  # A, B, C, or D",
      "execution_count": null,
      "outputs": [],
      "id": "conditionals-023"
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": "## M02 | Equality test\n\nWhich operator tests equality?\n\nA. =\nB. =>\nC. ==\nD. !=",
      "id": "conditionals-024"
    },
    {
      "cell_type": "code",
      "metadata": {},
      "source": "m02 = \"\"  # A, B, C, or D",
      "execution_count": null,
      "outputs": [],
      "id": "conditionals-025"
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": "## M03 | Both conditions\n\nWhen is `x > 0 and x < 10` true?\n\nA. For every positive x\nB. Only at x = 10\nC. For negative x\nD. When x lies strictly between 0 and 10",
      "id": "conditionals-026"
    },
    {
      "cell_type": "code",
      "metadata": {},
      "source": "m03 = \"\"  # A, B, C, or D",
      "execution_count": null,
      "outputs": [],
      "id": "conditionals-027"
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": "## M04 | Either condition\n\nWhat is `False or True`?\n\nA. True\nB. False\nC. None\nD. An error",
      "id": "conditionals-028"
    },
    {
      "cell_type": "code",
      "metadata": {},
      "source": "m04 = \"\"  # A, B, C, or D",
      "execution_count": null,
      "outputs": [],
      "id": "conditionals-029"
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": "## M05 | An empty branch\n\nWhich statement can occupy a branch that does nothing?\n\nA. empty\nB. skip\nC. pass\nD. stop",
      "id": "conditionals-030"
    },
    {
      "cell_type": "code",
      "metadata": {},
      "source": "m05 = \"\"  # A, B, C, or D",
      "execution_count": null,
      "outputs": [],
      "id": "conditionals-031"
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": "## M06 | Branch structure\n\nWhat must follow an if header and colon?\n\nA. A closing brace\nB. An indented body\nC. A return statement\nD. An import",
      "id": "conditionals-032"
    },
    {
      "cell_type": "code",
      "metadata": {},
      "source": "m06 = \"\"  # A, B, C, or D",
      "execution_count": null,
      "outputs": [],
      "id": "conditionals-033"
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": "## M07 | First matching branch\n\nIf two conditions in an if/elif chain are true, which bodies run?\n\nA. Both\nB. Neither\nC. Only the last\nD. Only the first matching body",
      "id": "conditionals-034"
    },
    {
      "cell_type": "code",
      "metadata": {},
      "source": "m07 = \"\"  # A, B, C, or D",
      "execution_count": null,
      "outputs": [],
      "id": "conditionals-035"
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": "## M08 | Nested decisions\n\nWhen is an inner if reached inside an outer if body?\n\nA. When the outer condition is true\nB. Before the outer condition is tested\nC. Only when the outer condition is false\nD. Always",
      "id": "conditionals-036"
    },
    {
      "cell_type": "code",
      "metadata": {},
      "source": "m08 = \"\"  # A, B, C, or D",
      "execution_count": null,
      "outputs": [],
      "id": "conditionals-037"
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": "## M09 | Exception path\n\nIf float conversion raises an error inside try, control moves to which block?\n\nA. else automatically\nB. The next line of try\nC. except\nD. The beginning of the script",
      "id": "conditionals-038"
    },
    {
      "cell_type": "code",
      "metadata": {},
      "source": "m09 = \"\"  # A, B, C, or D",
      "execution_count": null,
      "outputs": [],
      "id": "conditionals-039"
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": "## M10 | Guardian ordering\n\nWhich expression guards division when y is zero?\n\nA. x / y > 2 and y != 0\nB. y != 0 and x / y > 2\nC. x / y > 2 or y == 0\nD. y == 0 and x / y > 2",
      "id": "conditionals-040"
    },
    {
      "cell_type": "code",
      "metadata": {},
      "source": "m10 = \"\"  # A, B, C, or D",
      "execution_count": null,
      "outputs": [],
      "id": "conditionals-041"
    }
  ]
}
