{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "id": "986fb4ce",
   "source": "# Homework\n\n- Use this assignment to practice the chapter's concepts and coding skills in runnable Python code.\n- Sign in from the account menu before you start working on the assignment.\n- Write and run your code directly in the coding cells.\n- Multiple submissions are allowed. The highest score is kept.\n\n<style>\n.tf-options { display: flex; gap: 1.25rem; flex-wrap: wrap; margin: 0.4rem 0 0.15rem; }\n.tf-options label { display: inline-flex; align-items: center; gap: 0.35rem; cursor: pointer; font: inherit; font-weight: 400; line-height: normal; }\n.tf-options input { margin: 0; }\n.cell:has(.tf-options) { margin-bottom: 0.1rem; }\n.cell:has(.tf-options) + .cell { margin-top: 0; margin-bottom: 1.65rem; }\n.cell:has(.tf-options) + .cell details { margin-top: 0; }\n</style>\n\n<div data-assignment-answers=\"ch01-homework\"></div>\n"
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "id": "f91e0083",
   "source": "::::{div} assignment-question-card\n\n**1. Programs**\n\nA manager asks an analyst to write a repeatable set of Python instructions that prepares the weekly sales report. Calling that set of instructions a program is appropriate.\n\n<div class=\"tf-options\" role=\"radiogroup\" aria-label=\"Question 1 answer\">\n  <label><input type=\"radio\" name=\"ch01-hw-q1\" value=\"true\"> True</label>\n  <label><input type=\"radio\" name=\"ch01-hw-q1\" value=\"false\"> False</label>\n</div>\n\n::::\n"
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "id": "c2477645",
   "source": "::::{div} assignment-question-card\n\n**2. Algorithms**\n\nBefore assigning a dashboard task, a team lead sketches the step-by-step logic for cleaning, grouping, and summarizing customer records without choosing Python syntax yet. That sketch can still be an algorithm.\n\n<div class=\"tf-options\" role=\"radiogroup\" aria-label=\"Question 2 answer\">\n  <label><input type=\"radio\" name=\"ch01-hw-q2\" value=\"true\"> True</label>\n  <label><input type=\"radio\" name=\"ch01-hw-q2\" value=\"false\"> False</label>\n</div>\n\n::::\n"
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "id": "2686eb9d",
   "source": "::::{div} assignment-question-card\n\n**3. Syntax**\n\nA supervisor reviews a script that has the right business logic but missing parentheses and mismatched quotes. Because the business logic is correct, Python syntax rules do not matter for whether the script can run.\n\n<div class=\"tf-options\" role=\"radiogroup\" aria-label=\"Question 3 answer\">\n  <label><input type=\"radio\" name=\"ch01-hw-q3\" value=\"true\"> True</label>\n  <label><input type=\"radio\" name=\"ch01-hw-q3\" value=\"false\"> False</label>\n</div>\n\n::::\n"
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "id": "e37f694c",
   "source": "::::{div} assignment-question-card\n\n**4. Comments**\n\nA project manager asks the team to add notes explaining why a pricing rule exists. If those notes are written as Python lines starting with `#`, they normally document the script without changing what runs.\n\n<div class=\"tf-options\" role=\"radiogroup\" aria-label=\"Question 4 answer\">\n  <label><input type=\"radio\" name=\"ch01-hw-q4\" value=\"true\"> True</label>\n  <label><input type=\"radio\" name=\"ch01-hw-q4\" value=\"false\"> False</label>\n</div>\n\n::::\n"
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "id": "4b755abb",
   "source": "::::{div} assignment-question-card\n\n**5. Input Values**\n\nAn operations form asks for the number of units to reorder. If a Python script reads that value with `input()`, Python automatically treats it as a number whenever the next line multiplies it by a price.\n\n<div class=\"tf-options\" role=\"radiogroup\" aria-label=\"Question 5 answer\">\n  <label><input type=\"radio\" name=\"ch01-hw-q5\" value=\"true\"> True</label>\n  <label><input type=\"radio\" name=\"ch01-hw-q5\" value=\"false\"> False</label>\n</div>\n\n::::\n"
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "tags": [
     "thebe-interactive"
    ]
   },
   "id": "89434794",
   "outputs": [],
   "source": [
    "### 6. Fix a Syntax Error\n",
    "# The code below contains one syntax error, a missing closing parenthesis:\n",
    "# print(\"ThinkPy homework\"\n",
    "# print(\"Chapter 1 is about first programs\")\n",
    "# Write the corrected version so it prints exactly:\n",
    "# ThinkPy homework\n",
    "# Chapter 1 is about first programs\n",
    "### Your code starts here.\n",
    "\n",
    "\n",
    "\n",
    "### Your code ends here.\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "tags": [
     "thebe-interactive"
    ]
   },
   "id": "39f533ba",
   "outputs": [],
   "source": [
    "### 7. Fix a NameError\n",
    "# A program that prints an order total failed with this error message:\n",
    "# NameError: name 'quantty' is not defined\n",
    "# Read the last line of the message and find the misspelled name in the broken code below.\n",
    "# Write the corrected program so that it prints exactly:\n",
    "# Total: 30\n",
    "#\n",
    "# Broken code (do not run it):\n",
    "# price = 10\n",
    "# quantity = 3\n",
    "# print(\"Total:\", price * quantty)\n",
    "### Your code starts here.\n",
    "\n",
    "\n",
    "\n",
    "### Your code ends here."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "tags": [
     "thebe-interactive"
    ]
   },
   "id": "c45a776d",
   "outputs": [],
   "source": [
    "### 8. Inspect Converted Input\n",
    "# The lab converted text input to a number. Here, confirm the type changes after conversion.\n",
    "# Convert quantity_text to an integer named quantity.\n",
    "# Print exactly:\n",
    "# Before: <class 'str'>\n",
    "# After: <class 'int'>\n",
    "\n",
    "quantity_text = \"42\"\n",
    "\n",
    "### Your code starts here.\n",
    "\n",
    "\n",
    "\n",
    "### Your code ends here.\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "tags": [
     "thebe-interactive"
    ]
   },
   "id": "428593ad",
   "outputs": [],
   "source": [
    "### 9. Write a Stakeholder Message\n",
    "# The lab practiced formatted output. Here, write a short message for a business reader.\n",
    "# Use an f-string and the variables below.\n",
    "# Print exactly:\n",
    "# North region needs 6 follow-up calls.\n",
    "\n",
    "region = \"North\"\n",
    "follow_up_calls = 6\n",
    "\n",
    "### Your code starts here.\n",
    "\n",
    "\n",
    "\n",
    "### Your code ends here.\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "tags": [
     "thebe-interactive"
    ]
   },
   "id": "45cdb718",
   "outputs": [],
   "source": [
    "### 10. Build an Audit Tag\n",
    "# The lab printed separate number-system and character-code values.\n",
    "# Here, combine an initial, its numeric code, and a hex item number into one tag.\n",
    "# Use ord(), str(), and hex().\n",
    "# Print exactly:\n",
    "# Audit tag: A-65-0xff\n",
    "\n",
    "initial = \"A\"\n",
    "item_number = 255\n",
    "\n",
    "### Your code starts here.\n",
    "\n",
    "\n",
    "\n",
    "### Your code ends here.\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "id": "0c719486",
   "source": "<style>\n.ch01-homework-submit { max-width: 900px; margin-top: 1rem; }\n.ch01-homework-feedback-list { display: grid; gap: 0.45rem; margin: 0 0 1rem; }\n.ch01-homework-feedback-list[hidden] { display: none; }\n.ch01-homework-feedback-row { border: 1px solid #d8dee4; border-radius: 6px; padding: 0.55rem 0.65rem; }\n.ch01-homework-feedback-row strong { margin-right: 0.35rem; }\n.ch01-homework-feedback-row.correct { border-color: #aceebb; background: #dafbe1; color: #116329; }\n.ch01-homework-feedback-row.incorrect { border-color: #ffcecb; background: #ffebe9; color: #cf222e; }\n.ch01-homework-actions { display: flex; gap: 0.75rem; align-items: center; margin: 1rem 0 0; flex-wrap: wrap; }\n.ch01-homework-actions button { border: 1px solid #176de8; border-radius: 6px; background: #176de8; color: white; padding: 0.55rem 0.9rem; cursor: pointer; font-weight: 600; }\n.ch01-homework-actions button.secondary { background: white; color: #176de8; }\n.ch01-homework-actions.is-locked { opacity: 0.55; }\n.ch01-homework-actions button:disabled { cursor: not-allowed; opacity: 0.65; }\n.ch01-homework-result { font-weight: 700; }\n</style>\n\n<div class='ch01-homework-submit' id='ch01-homework-submit-panel'>\n  <div class='ch01-homework-feedback-list' aria-live='polite' hidden>\n    <div class='ch01-homework-feedback-row' data-question='q1'><strong>Question 1</strong><span></span></div>\n    <div class='ch01-homework-feedback-row' data-question='q2'><strong>Question 2</strong><span></span></div>\n    <div class='ch01-homework-feedback-row' data-question='q3'><strong>Question 3</strong><span></span></div>\n    <div class='ch01-homework-feedback-row' data-question='q4'><strong>Question 4</strong><span></span></div>\n    <div class='ch01-homework-feedback-row' data-question='q5'><strong>Question 5</strong><span></span></div>\n    <div class='ch01-homework-feedback-row' data-question='q6'><strong>Question 6</strong><span></span></div>\n    <div class='ch01-homework-feedback-row' data-question='q7'><strong>Question 7</strong><span></span></div>\n    <div class='ch01-homework-feedback-row' data-question='q8'><strong>Question 8</strong><span></span></div>\n    <div class='ch01-homework-feedback-row' data-question='q9'><strong>Question 9</strong><span></span></div>\n    <div class='ch01-homework-feedback-row' data-question='q10'><strong>Question 10</strong><span></span></div>\n  </div>\n  <div class='ch01-homework-actions'>\n    <button type='button' id='ch01-homework-submit' disabled>Submit</button>\n    <button type='button' class='secondary' id='ch01-homework-reset'>Clear Results</button>\n    <span class='ch01-homework-result' id='ch01-homework-result' aria-live='polite'></span>\n  </div>\n</div>\n\n<script>\n(function () {\n  var homeworkId = 'ch01-homework';\n\n  function getParams() {\n    var params = new URLSearchParams(window.location.search);\n    return {\n      canvas_course_id: params.get('canvas_course_id') || params.get('course_id') || '',\n      canvas_assignment_id: params.get('canvas_assignment_id') || params.get('assignment_id') || '',\n      canvas_user_id: params.get('canvas_user_id') || params.get('user_id') || ''\n    };\n  }\n\n  function loadSessionIdentity(identity, actions, submit, result) {\n    submit.disabled = true;\n    actions.classList.add('is-locked');\n    result.textContent = 'Sign in from the account menu to submit this assignment.';\n    return fetch('/api/v1/session.php', { credentials: 'same-origin' })\n      .then(function (response) { return response.ok ? response.json() : null; })\n      .then(function (payload) {\n        if (!payload || !payload.authenticated || !payload.identity) return identity;\n        identity.canvas_user_id = payload.identity.canvas_user_id || identity.canvas_user_id || '';\n        identity.student_identifier = payload.identity.student_identifier || '';\n        submit.disabled = false;\n        actions.classList.remove('is-locked');\n        result.textContent = payload.identity.display_name ? 'Signed in as ' + payload.identity.display_name + '.' : 'Signed in.';\n        return identity;\n      })\n      .catch(function () { return identity; });\n  }\n\n  function codeFromCell(cell) {\n    var codeMirror = cell.querySelector('.CodeMirror');\n    if (codeMirror && codeMirror.CodeMirror && typeof codeMirror.CodeMirror.getValue === 'function') {\n      return codeMirror.CodeMirror.getValue();\n    }\n    var cmContent = cell.querySelector('.cm-content');\n    if (cmContent) {\n      return Array.from(cmContent.querySelectorAll('.cm-line')).map(function (line) { return line.textContent; }).join('\\n');\n    }\n    var textarea = cell.querySelector('textarea');\n    if (textarea) return textarea.value;\n    var pre = cell.querySelector('.cell_input pre');\n    return pre ? pre.textContent : '';\n  }\n\n  function collectCodeCells() {\n    var cells = Array.from(document.querySelectorAll('div.cell.tag_thebe-interactive')).slice(0, 5);\n    var code = {};\n    cells.forEach(function (cell, index) {\n      code['q' + (index + 6)] = codeFromCell(cell);\n    });\n    return code;\n  }\n\n  function collectAnswers() {\n    var answers = {};\n    ['q1', 'q2', 'q3', 'q4', 'q5'].forEach(function (question) {\n      var selected = document.querySelector('input[name=ch01-hw-' + question + ']:checked');\n      answers[question] = selected ? selected.value : '';\n    });\n    return answers;\n  }\n\n  function applyFeedback(panel, feedback) {\n    var list = panel.querySelector('.ch01-homework-feedback-list');\n    if (list) list.hidden = false;\n    ['q1', 'q2', 'q3', 'q4', 'q5', 'q6', 'q7', 'q8', 'q9', 'q10'].forEach(function (question) {\n      var row = panel.querySelector('[data-question=' + question + ']');\n      var message = row ? row.querySelector('span') : null;\n      var item = feedback ? feedback[question] : null;\n      if (!row || !message || !item) return;\n      row.classList.remove('correct', 'incorrect');\n      message.textContent = item.message + ' (' + item.score + ' / ' + item.max_score + ')';\n      row.classList.add(item.correct ? 'correct' : 'incorrect');\n    });\n  }\n\n  function clearFeedback(panel) {\n    var list = panel.querySelector('.ch01-homework-feedback-list');\n    if (list) list.hidden = true;\n    panel.querySelectorAll('.ch01-homework-feedback-row').forEach(function (row) {\n      row.classList.remove('correct', 'incorrect');\n      var message = row.querySelector('span');\n      if (message) message.textContent = '';\n    });\n  }\n\n  function saveAttempt(payload) {\n    return fetch('/api/v1/homework-attempts.php', {\n      method: 'POST',\n      headers: { 'Content-Type': 'application/json' },\n      credentials: 'same-origin',\n      body: JSON.stringify(payload)\n    }).then(function (response) {\n      return response.text().then(function (text) {\n        var saved = text ? JSON.parse(text) : null;\n        if (!response.ok || !saved || !saved.ok) throw new Error(saved && (saved.message || saved.error) ? (saved.message || saved.error) : 'Submission API is unavailable.');\n        return saved;\n      });\n    });\n  }\n\n  function initHomeworkSubmit() {\n    var panel = document.getElementById('ch01-homework-submit-panel');\n    if (!panel || panel.dataset.ready === 'true') return;\n    panel.dataset.ready = 'true';\n\n    var identity = getParams();\n    var result = document.getElementById('ch01-homework-result');\n    var submit = document.getElementById('ch01-homework-submit');\n    var actions = panel.querySelector('.ch01-homework-actions');\n    var reset = document.getElementById('ch01-homework-reset');\n\n    loadSessionIdentity(identity, actions, submit, result);\n\n    submit.addEventListener('click', function () {\n      clearFeedback(panel);\n      result.textContent = 'Submitting homework.';\n      submit.disabled = true;\n      saveAttempt({\n        homework_id: homeworkId,\n        answers: collectAnswers(),\n        code: collectCodeCells(),\n        canvas_course_id: identity.canvas_course_id,\n        canvas_assignment_id: identity.canvas_assignment_id,\n        canvas_user_id: identity.canvas_user_id\n      })\n        .then(function (payload) {\n          if (payload.feedback) applyFeedback(panel, payload.feedback);\n          var best = (payload.best_score === null || typeof payload.best_score === 'undefined') ? payload.score : payload.best_score;\n          var attempt = (payload.attempt_count === null || typeof payload.attempt_count === 'undefined') ? '' : ' Attempt ' + payload.attempt_count + '.';\n          result.textContent = 'Submission saved.' + attempt + ' Score: ' + payload.score + ' / ' + payload.max_score + '. Best score: ' + best + ' / ' + payload.max_score + '. Canvas sync: ' + payload.canvas_sync_status + '.';\n        })\n        .catch(function (error) {\n          result.textContent = 'Could not save this attempt: ' + error.message;\n        })\n        .finally(function () {\n          submit.disabled = false;\n        });\n    });\n\n    reset.addEventListener('click', function () {\n      clearFeedback(panel);\n      result.textContent = '';\n    });\n  }\n\n  if (document.readyState === 'loading') {\n    document.addEventListener('DOMContentLoaded', initHomeworkSubmit);\n  } else {\n    initHomeworkSubmit();\n  }\n})();\n</script>"
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "name": "python",
   "version": "3.11.0"
  },
  "press_solutions_stripped": true
 },
 "nbformat": 4,
 "nbformat_minor": 5
}