{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "l01-c000",
   "metadata": {},
   "source": [
    "# Lecture 1 - Algorithmic thinking\n",
    "\n",
    "<!-- <a rel=\"license\" href=\"https://creativecommons.org/licenses/by/4.0/\" target=\"_blank\"><img alt=\"Creative Commons Licence\" style=\"border-width:0\" src=\"https://licensebuttons.net/l/by/4.0/88x31.png\" title=\"This work is licensed under a Creative Commons Attribution 4.0 International License.\" align=\"right\"/></a> -->\n",
    "\n",
    "**Authors:** Fiona McNeill, Matteo Degiacomi, Rayo Verweij, and Nigel Topham\n",
    "\n",
    "## Introduction\n",
    "\n",
    "Welcome to the first self-guided Intro to Programming notebook! These notebooks present the material you're expected to know from each lecture and give you some extra exercises to practice on your own. While they will explain everything, we highly recommend watching the relevant lecture for each material first before coming here, if you haven't already attended the live version.\n",
    "\n",
    "### Learning outcomes\n",
    "\n",
    "In this notebook, we'll discuss:\n",
    "\n",
    "- what a Python program typically looks like\n",
    "- displaying text and values with `print()` and collecting text with `input()`\n",
    "- assigning and using variables\n",
    "- using arithmetic operators to perform calculations\n",
    "- planning programs using decomposition and pseudocode\n",
    "- documenting your code with comments\n",
    "\n",
    "### Using the notebook\n",
    "\n",
    "Remember, this notebook is _yours_! You can edit each cell, for example by adding new examples or changing values to see what happens. Feel free to experiment!\n",
    "\n",
    "A common approach for learning how to read and write code in a structured way is called **PRIMM**:\n",
    "1. **Predict**: before running the code, look at it - do you understand each line? What do you think will happen?\n",
    "2. **Run**: now run the code and see what output you get.\n",
    "3. **Investigate**: did it go as you thought it would? If not, can you figure out why?\n",
    "4. **Modify**: now change the code and once again try to predict the output. If you got it wrong the first time, did you get it right now?\n",
    "5. **Make**: finally, try to write your own program from scratch using the skills you learned.\n",
    "\n",
    "You can apply steps 1-4 to each code cell in the notebook and many will have prediction prompts to help you get started. In addition, there will be challenges that ask you to put everything together yourself and execute step 5. There will often be multiple ways to solve these, but each challenge has a potential answer listed.\n",
    "\n",
    "### Navigating the notebook\n",
    "\n",
    "A Jupyter notebook can be operated with your keyboard. Here is a **cheat sheet**:\n",
    "- To run the currently highlighted cell and move focus to the next cell, hold <kbd>&#x21E7; Shift</kbd> and press <kbd>&#x23ce; Enter</kbd>;\n",
    "- To run the currently highlighted cell and keep focus in the same cell, hold <kbd>&#x21E7; Ctrl</kbd> and press <kbd>&#x23ce; Enter</kbd>;\n",
    "- To create a cell under the one currently highlighted, press <kbd>B</kbd>;\n",
    "- To delete the currently highlighted cell, press <kbd>X</kbd> (be careful with this one!);\n",
    "- To get help for a specific function, place the cursor within the function's brackets, hold <kbd>&#x21E7; Shift</kbd>, and press <kbd>&#x21E5; Tab</kbd>.\n",
    "\n",
    "Watch out: **code cells remember what happened in cells before**. So, especially in the more complicated notebooks later down the line, make sure to always run **every** cell from top to bottom, as one might rely on a piece of code that came before it!\n",
    "\n",
    "\n",
    "### Further reading\n",
    "\n",
    "- [Python: an informal introduction](https://docs.python.org/3/tutorial/introduction.html)\n",
    "- [Python built-in functions](https://docs.python.org/3/library/functions.html)\n",
    "- [Pedagogy Quick Reads: Using PRIMM to structure programming lessons](https://media.teachcomputing.org/QR_11_PRIMM_a57d70eb85.pdf) - written for teachers, but outlines the ideas behind PRIMM quite well\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "d9544523-1716-461a-9abd-e46dad405c7a",
   "metadata": {},
   "source": [
    "## 1. Programs"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "l01-c001",
   "metadata": {},
   "source": [
    "A **program** is a **set of instructions to a computer**. Normally, instructions are carried out individually, one after another, from top to bottom.\n",
    "\n",
    "**Predict:** Read the following program line by line. What do you think it does? Without running the cell, write down its output line."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "l01-c002",
   "metadata": {},
   "outputs": [],
   "source": [
    "ticket_price = 8\n",
    "number_of_tickets = 3\n",
    "total = ticket_price * number_of_tickets\n",
    "print(total)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "l01-c003",
   "metadata": {},
   "source": [
    "Let's go through it piece by piece.\n",
    "\n",
    "`=` is the **assignment operator**. It gives a **name** to an **object** so we can easily reuse it later. This is called **assigning a variable**.\n",
    "\n",
    "The program above has three variables:\n",
    "* `ticket_price`, which is assigned the value `8`;\n",
    "* `number_of_tickets`, which is assigned the value `3`;\n",
    "* `total`, which is assigned to the variable `ticket_price` times the variable `number_of_tickets` - that is, 8*3 or 24.\n",
    "\n",
    "You can name a variable almost _anything you want_ using any alphanumeric characters (`A-z` and `0-9`) as well as underscores (the `_` character). Variables in Python cannot *start* with a number, however.\n",
    "\n",
    "It's not always easy to come up with good, descriptive names for variables - in fact, it is a common computer science joke that there are only two hard problems in programming: cache invalidation, naming things, and off-by-one errors.\n",
    "\n",
    "Finally, `print()` is a function that sends **output** to the screen.\n",
    "\n",
    "In addition to printing variables you can print numbers directly, as well as text in quotes, calculations, and even multiple things when separated by commas."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "l01-c004",
   "metadata": {},
   "outputs": [],
   "source": [
    "print(\"Hello from Edinburgh\")\n",
    "print(6 + 4)\n",
    "distance_km = 7\n",
    "print(\"Distance in kilometres:\", distance_km)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "c36ba9dc",
   "metadata": {},
   "source": [
    "Wait, text in quotes?\n",
    "\n",
    "In programming, pieces of text are called **strings**. Strings, like numbers, are **objects**. Variables, as said, are **names that refer to objects**.\n",
    "\n",
    "As discussed above, variables are written using a combination of alphanumeric characters and underscores, and nothing else.\n",
    "\n",
    "Strings, however, are **always written in quotation marks**. These can be either single (`'`) or double (`\"`) quotation marks. Within these, you can use _any_ character you want to use. The quotation marks essentially tell Python: _\"ignore anything inside of me, and just treat me as a large piece of human text\"_.\n",
    "\n",
    "**Predict:** What happens when you try to run the code below? What are the **two** ways in which you could fix it?"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "4ee268dc",
   "metadata": {},
   "outputs": [],
   "source": [
    "print(morning)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "41de62d4",
   "metadata": {},
   "source": [
    "<details><summary><b>Click to see the two possible solutions</b></summary>\n",
    "\n",
    "This code throws an error because `morning` is a variable that has not been assigned to any value.\n",
    "\n",
    "How to fix the code depends on what you want `morning` to be.\n",
    "\n",
    "If you just want to print the text \"morning\", then you should update the code to use quotation marks:\n",
    "\n",
    "```python\n",
    "print(\"morning\")\n",
    "```\n",
    "\n",
    "But if you want `morning` to be a variable, then you should **assign** it before using:\n",
    "```python\n",
    "morning = \"Good morning, all!\"\n",
    "print(morning)\n",
    "```\n",
    "</details>\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "c7c471ee",
   "metadata": {},
   "source": [
    "What if you want to add the quotation marks themselves to a string? We can do this by **escaping** them: writing a backslash `\\` in front of a character will make Python treat it as a normal character instead of a special one.\n",
    "\n",
    "This also works for the backslash itself! Take a look at the following statements to see how it works:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "ba2bb760",
   "metadata": {},
   "outputs": [],
   "source": [
    "print(\"I can use 'single quotes' when my string has double quotes\")\n",
    "print(\"But when I want to use \\\"double quotes\\\", I have to \\\"escape\\\" them with backslashes or the string will end :O\")\n",
    "print('The same is true when using \\'single quotes\\' inside a string made with single quotes...')\n",
    "print(\"...and it also works for the \\\\ backslash character itself!\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "6fcec045",
   "metadata": {},
   "source": [
    "### Algorithms\n",
    "An **algorithm** is a **set of instructions that describes how to solve a problem**.\n",
    "\n",
    "A program can **implement** an algorithm, or many algorithms, or none at all. For example, the following program:\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "5f7ac263",
   "metadata": {},
   "outputs": [],
   "source": [
    "print(\"Hello world\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "c346270d",
   "metadata": {},
   "source": [
    "...does not contain an algorithm at all, as it simply displays some words, instead of using some kind of logic to solve a problem.\n",
    "\n",
    "Vice versa, algorithms do not have to be implemented by programs - they could be carried out by humans, for example.\n",
    "\n",
    "Say that I have the problem of \"_if I have five biscuits and give two away, how many do I have left?_\"\n",
    "\n",
    "An algorithm to solve this problem would be:\n",
    "1. Take the number 5\n",
    "2. Subtract 2 from it\n",
    "\n",
    "...which can be carried out by most humans and computers alike. However, when we want to solve this problem using Python, we have to write a specific program for it:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "e08c8677",
   "metadata": {},
   "outputs": [],
   "source": [
    "print(\"Biscuits left:\", 5 - 2)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "4837076c",
   "metadata": {},
   "source": [
    "Another way to think about is this: say we have to translate a piece of code from one programming language to another. While the algorithm will likely stay the same, the new program might look completely differently from the original one."
   ]
  },
  {
   "cell_type": "markdown",
   "id": "l01-c005",
   "metadata": {},
   "source": [
    "### Arithmetic operators\n",
    "\n",
    "All right, back to coding. Python uses `+`, `-`, `*`, and `/` for addition, subtraction, multiplication, and division, as well as `(` and `)` to group operations together.\n",
    "\n",
    "As per usual, multiplication and division happen _before_ addition and subtraction.\n",
    "\n",
    "**Predict:** Find pen and paper or your favourite note-taking app. What do you expect the output of each of the following lines to be?"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "l01-c006",
   "metadata": {},
   "outputs": [],
   "source": [
    "print(2 + 3 * 4)\n",
    "print((2 + 3) * 4)\n",
    "minutes = 2 * 60 + 15\n",
    "print(\"Total minutes:\", minutes)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "l01-c007",
   "metadata": {
    "tags": [
     "exercise"
    ]
   },
   "source": [
    "<div class=\"alert alert-success\">\n",
    "<b>Try it yourself: repair the sequence</b>\n",
    "\n",
    "We're trying to calculate how much it is to book three £5 tickets including a one-time £2 booking fee... but the lines of the following program have gotten all scrambled! Reorder them so they are in the right position and finish the line starting with `total`.\n",
    "</div>\n",
    "\n",
    "<div class=\"alert alert-info\"><b>Note: efficient editing</b>\n",
    "    \n",
    "When your cursor is in a line of text in a code cell, holding the Alt or Option key while pressing the up and down arrows allows you to quickly move lines of code around! This works not just in notebooks but in most code editors as well.\n",
    "</div>"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "l01-c008",
   "metadata": {
    "tags": [
     "exercise"
    ]
   },
   "outputs": [],
   "source": [
    "total = ?\n",
    "ticket_price = 5\n",
    "print(\"Total:\", total)\n",
    "booking_fee = 2\n",
    "quantity = 3"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "l01-c009",
   "metadata": {
    "tags": [
     "solution"
    ]
   },
   "source": [
    "<details>\n",
    "<summary><b>Click for the solution</b></summary>\n",
    "\n",
    "```python\n",
    "ticket_price = 5\n",
    "booking_fee = 2\n",
    "quantity = 3\n",
    "total = ticket_price * quantity + booking_fee\n",
    "print(\"Total:\", total)\n",
    "```\n",
    "\n",
    "Note that it does not matter in which order the first three lines appear.\n",
    "\n",
    "</details>\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "6da541f0-f746-46d3-8030-a90b463bef90",
   "metadata": {},
   "source": [
    "## 2. Input, process, output"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "l01-c010",
   "metadata": {},
   "source": [
    "`input()` displays a prompt and waits for the user to enter text.\n",
    "\n",
    "**Predict:** If you enter `Sam`, which value is stored in `name`, and what is printed?\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "l01-c011",
   "metadata": {},
   "outputs": [],
   "source": [
    "name = input(\"What is your name? \")\n",
    "print(\"Welcome\", name)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "l01-c012",
   "metadata": {},
   "source": [
    "Many programs can be understood as having three stages:\n",
    "\n",
    "1. **Input:** obtain information from the user;\n",
    "2. **Process:** transform or combine that information, often by implementing some kind of algorithm;\n",
    "3. **Output:** communicate the result back to the user.\n",
    "\n",
    "Identifying each stage can help you reason about what a program needs and whether a step is missing.\n",
    "\n",
    "As you plan a program, it can help to draw three boxes labelled input, process, and output, then place each planned step in a box. If a value is needed in the process box but never appears as an input, then you have found a gap in your program design!"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "l01-c013",
   "metadata": {},
   "outputs": [],
   "source": [
    "# Input\n",
    "walk_minutes = 18\n",
    "bus_minutes = 11\n",
    "\n",
    "# Process\n",
    "time_saved = walk_minutes - bus_minutes\n",
    "\n",
    "# Output\n",
    "print(\"Minutes saved if I take the bus:\", time_saved)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "9ff65c5d",
   "metadata": {},
   "source": [
    "User input can come in all sorts of shapes and sizes, so before we can transform our data we often have to put it in a consistent format that we can work with. This is called **data cleaning**. Throughout this course we'll look at different ways of doing data cleaning, but for now we'll limit ourselves to a straightforward example: trimming useless whitespace from input.\n",
    "\n",
    "When adding `.strip()` to the end of an `input()` statement, Python removes any whitespace (spaces, tabs, etc.) from the beginning and end of the user's input.\n",
    "\n",
    "**Predict:** Run the following program and add a few spaces before you type your name. What will the output be?"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "294b1f8f",
   "metadata": {},
   "outputs": [],
   "source": [
    "name_clean = input(\"What is your name? \").strip()\n",
    "print(\"Welcome\", name_clean)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "5410f5e6",
   "metadata": {},
   "source": [
    "You can call `input()` as many times as you'd like and Python will ask for user input as many times in sequence."
   ]
  },
  {
   "cell_type": "markdown",
   "id": "l01-c014",
   "metadata": {
    "tags": [
     "exercise"
    ]
   },
   "source": [
    "<div class=\"alert alert-success\">\n",
    "<b>Try it yourself: multiple pieces of input</b>\n",
    "\n",
    "Create an order receipt for a customer in a café. Ask them first what they'd like to drink and second what kind of cake they'd like; clean the input of excess whitespace; and print one sentence that includes the full order.\n",
    "</div>\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "l01-c015",
   "metadata": {
    "tags": [
     "exercise"
    ]
   },
   "outputs": [],
   "source": [
    "# Write your program here\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "l01-c016",
   "metadata": {
    "tags": [
     "solution"
    ]
   },
   "source": [
    "<details>\n",
    "<summary><b>Click for a possible solution</b></summary>\n",
    "\n",
    "```python\n",
    "drink_choice = input(\"Which drink would you like? \").strip()\n",
    "cake_choice = input(\"Which cake would you like? \").strip()\n",
    "\n",
    "print(\"You ordered:\", drink_choice, \"and\", cake_choice)\n",
    "```\n",
    "</details>\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "ea41c3fc",
   "metadata": {},
   "source": [
    "<div class=\"alert alert-info\">\n",
    "<b>Note: spaces in print statements</b>\n",
    "\n",
    "As noted earlier, you can print multiple things at once by separating them with commas inside of `print()`. But have you noticed that when doing this, Python automatically adds spaces between each part of the output?\n",
    "\n",
    "`input()`, however, does **not** automatically add a space between the question to the user and the input that they type.\n",
    "\n",
    "(When using Python in Noteable, it appears like there is, because Noteable adds some whitespace between the prompt and the user input. However, this is not the space *character*, and when using Python on your local machine, you'll see that the user input immediately follows the prompt unless you add a space at the end of the prompt.)\n",
    "</div>\n",
    "\n",
    "**Predict:** Which of the following lines will have spaces, and where?"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "040f2db8",
   "metadata": {},
   "outputs": [],
   "source": [
    "favourite = input(\"Your favourite number:\")\n",
    "runner_up = input(\"Your almost-favourite number: \")\n",
    "\n",
    "print(\"The best:\", favourite)\n",
    "print(\"Not quite the best: \", runner_up)\n",
    "print(\"Both once more, because we like them so much:\", favourite, runner_up)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "l01-c017",
   "metadata": {},
   "source": [
    "## 3. Decomposition and pseudocode"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "824ed5e1-d7e6-4e33-b078-2203aa0b2bad",
   "metadata": {},
   "source": [
    "When writing longer programs, it's easy to get lost in lines of code, especially when working on it at different times or with different people. It's therefore often a good idea to break them into smaller parts and plan them in ordinary language first.\n",
    "\n",
    "**Decomposition** means breaking a problem into smaller, manageable parts. By breaking down large programs into smaller ones that can work independently and be reused in different contexts, code can become a lot easier to write and understand.\n",
    "\n",
    "**Pseudocode** is what we call human language that is written *like* code, but without any of the peculiarities of the **syntax** (the rules) of the language you are using. What useful pseudocode is like differs for everyone, but a common pattern is to use clear and capitalised verbs at the start of a sentence:\n",
    "\n",
    "```text\n",
    "ASK the user for their name\n",
    "STORE a ticket price and quantity\n",
    "CALCULATE price multiplied by quantity\n",
    "DISPLAY the name and calculated cost\n",
    "```\n",
    "\n",
    "This plan exposes the inputs and the order of operations without getting distracted by the Python of it all."
   ]
  },
  {
   "cell_type": "markdown",
   "id": "l01-c020",
   "metadata": {
    "tags": [
     "exercise"
    ]
   },
   "source": [
    "<div class=\"alert alert-success\">\n",
    "<b>Try it yourself: planning ahead</b>\n",
    "\n",
    "Write pseudocode for calculating the total number of minutes spent studying across three sessions of 45 minutes. Then translate it into Python.\n",
    "</div>\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "l01-c021",
   "metadata": {
    "tags": [
     "exercise"
    ]
   },
   "outputs": [],
   "source": [
    "# Pseudocode (start every line with a # so Python ignores it):\n",
    "\n",
    "# Python:\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "l01-c022",
   "metadata": {
    "tags": [
     "solution"
    ]
   },
   "source": [
    "<details>\n",
    "<summary><b>Click for a possible solution</b></summary>\n",
    "\n",
    "```python\n",
    "# STORE the number of sessions\n",
    "# STORE the minutes in each session\n",
    "# CALCULATE sessions multiplied by minutes per session\n",
    "# DISPLAY the total\n",
    "\n",
    "sessions = 3\n",
    "minutes_per_session = 45\n",
    "total_minutes = sessions * minutes_per_session\n",
    "print(\"Minutes studied:\", total_minutes)\n",
    "```\n",
    "</details>\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "l01-c023",
   "metadata": {},
   "source": [
    "## 4. Comments"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "785130f9-3efd-4e00-b8e5-ee699cf56e47",
   "metadata": {},
   "source": [
    "You have seen it in a few examples now: Python ignores any line that starts with a `#`. These lines are called **comments**.\n",
    "\n",
    "Comments are notes for the people who read the code, including your future self. Properly documenting your code is probably **the most important thing you can do to keep your code manageable and yourself sane**. It will save future-you time spent figuring out what your reasoning was; it will help your collaborators make sense of what you did; and it will even help you find mistakes as you're actively coding, as oftentimes, writing something in natural language will force you to look at it from a different perspective than just writing it in a programming language.\n",
    "\n",
    "When writing comments, try to write them in a way that gives a concise overview of information that is not immediately obvious from just reading the code."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "l01-c024",
   "metadata": {},
   "outputs": [],
   "source": [
    "# Prices are in pounds and exclude the optional donation\n",
    "entry_price = 12\n",
    "visitors = 4\n",
    "group_cost = entry_price * visitors\n",
    "print(\"Group cost:\", group_cost)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "l01-c025",
   "metadata": {},
   "source": [
    "Comments can also start at the end of a line that already has Python code:\n",
    "\n",
    "```python\n",
    "visitors = 4  # IMPORTANT: Need to update this later!!\n",
    "```\n",
    "\n",
    "Here too, Python will simply ignore everything that comes after the `#` until you start a new line.\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "94b89d2d-7713-4754-8dc8-9c8b2fb2c470",
   "metadata": {},
   "source": [
    "## Consolidation challenge"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "l01-consolidation-prompt",
   "metadata": {
    "tags": [
     "exercise"
    ]
   },
   "source": [
    "All right, let's try and put everything together in one final challenge. Try to build it in small pieces:\n",
    "* Consider what the input, process, and output stages are\n",
    "* Write pseudocode for each line you think you will need\n",
    "* Give your variables clear names\n",
    "* Add comments that explain your algorithm in human language"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "ad5fc8b5-9d49-49a9-b67c-70dd6e97df13",
   "metadata": {},
   "source": [
    "<div class=\"alert alert-success\">\n",
    "<b>Try it yourself: build an event budgeting program</b>\n",
    "    \n",
    "We're starting a catering business, <i>Algorithmic Appetites</i>, and that requires an event budgeting program. It should prompt the user to enter the organiser's name, the event name, and the venue, and then output a short budget summary. Make sure to clean away any excess whitespace. Store the number of attendees as 48 and the number of attendees with dietary requirements as 7. All attendees get a refreshment, which costs £4 per person, and all attendees <i>except</i> those with dietary requirements also get a meal*, at £12 per person. Write your algorithm in pseudocode as comments.\n",
    "\n",
    "\n",
    "<i>*Unfortunately, our programming skills have not yet reached the level where we can cater to people's dietary requirements. We will remedy this next week.</i>\n",
    "</div>"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "l01-consolidation-work",
   "metadata": {
    "tags": [
     "exercise"
    ]
   },
   "outputs": [],
   "source": [
    "# Pseudocode\n",
    "\n",
    "# Input\n",
    "\n",
    "# Process\n",
    "\n",
    "# Output\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "l01-consolidation-solution",
   "metadata": {
    "tags": [
     "solution"
    ]
   },
   "source": [
    "<details><summary><b>Click for a possible solution</b></summary>\n",
    "\n",
    "```python\n",
    "# ASK the user for the organiser's name\n",
    "# ASK the user for the event name\n",
    "# ASK the user for the venue\n",
    "# STORE the number of attendees\n",
    "# STORE the number of attendees with dietary requirements\n",
    "# STORE the refreshment price\n",
    "# STORE the meal price\n",
    "# CALCULATE the total cost of refreshments\n",
    "# CALCULATE how many people do NOT have a dietary requirement\n",
    "# CALCULATE the total cost of meals\n",
    "# CALCULATE the cost of refreshments plus meals\n",
    "# DISPLAY the event summary\n",
    "\n",
    "# Input\n",
    "organiser_name = input(\"Who is organising the event? \").strip()\n",
    "event_name = input(\"What is the event called? \").strip()\n",
    "venue_name = input(\"Where is the event taking place? \").strip()\n",
    "attendees_total = 48\n",
    "attendees_diet_req = 7\n",
    "cost_refreshment = 4\n",
    "cost_meal = 12\n",
    "\n",
    "# Process\n",
    "total_refreshments = attendees_total * cost_refreshment\n",
    "\n",
    "attendees_no_diet_req = attendees_total - attendees_diet_req # Only those without dietary requirements get a meal, as we're a bad event service\n",
    "total_meals = attendees_no_diet_req * cost_meal\n",
    "\n",
    "overall_total = total_refreshments + total_meals\n",
    "\n",
    "# Output\n",
    "print(\"Event:\", event_name)\n",
    "print(\"Organiser:\", organiser_name)\n",
    "print(\"Venue:\", venue_name)\n",
    "print(\"Total attendees:\", attendees_total)\n",
    "print(\"Dietary requirements:\", attendees_diet_req)\n",
    "print(\"This event will set you back:\", overall_total, \"pounds\")\n",
    "```\n",
    "</details>\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "l01-consolidation-summary",
   "metadata": {
    "tags": [
     "summary"
    ]
   },
   "source": [
    "## Summary\n",
    "\n",
    "Well done, that's the first notebook finished!\n",
    "\n",
    "### Key terms\n",
    "\n",
    "<dl>\n",
    "\t<dt><b>Program</b></dt>\n",
    "\t<dd>A set of instructions to a computer.</dd>\n",
    "\t<dt><b>Algorithm</b></dt>\n",
    "\t<dd>A set of instructions that describes how to solve a problem.</dd>\n",
    "\t<dt><b>Syntax</b></dt>\n",
    "\t<dd>The rules of the symbols of a programming language.</dd>\n",
    "\t<dt><b>Decomposition</b></dt>\n",
    "\t<dd>Breaking down a problem into smaller, more manageable chunks.</dd>\n",
    "\t<dt><b>Pseudocode</b></dt>\n",
    "\t<dd>Computer code written in human-readable format, preserving the exact instructions but ignoring a programming language's particular syntax.</dd>\n",
    "\t<dt><b>String</b></dt>\n",
    "\t<dd>A type of value that holds a piece of human text; enveloped in single or double quotation marks.</dd>\n",
    "\t<dt><b>Escaping characters</b></dt>\n",
    "\t<dd>In a string, using <code>\\</code> to indicate to Python to ignore a special character that follows.</dd>\n",
    "\t<dt><b>Variable</b></dt>\n",
    "\t<dd>A name that refers to an object.</dd>\n",
    "\t<dt><b>Variable assignment</b></dt>\n",
    "\t<dd>Giving an object a particular name that you can use to refer to it from your code.</dd>\n",
    "\t<dt><b>Comment</b></dt>\n",
    "\t<dd>A piece of code that is ignored by the computer, usually used to add human-readable descriptions.</dd>\n",
    "</dl>\n",
    "\n",
    "### New syntax\n",
    "\n",
    "<dl>\n",
    "\t<dt><code>=</code></dt>\n",
    "\t<dd>The assignment operator, giving a name to an object.</dd>\n",
    "\t<dt><code>'...'</code> and <code>\"...\"</code></dt>\n",
    "\t<dd>Denote a string.</dd>\n",
    "\t<dt><code>+</code>, <code>-</code>, <code>*</code>, <code>/</code></dt>\n",
    "\t<dd>Addition, subtraction, multiplication, and division operators, respectively.</dd>\n",
    "\t<dt><code>(</code> and <code>)</code> (in mathematical expressions)</dt>\n",
    "\t<dd>Parentheses that group mathematical expressions to be executed before the expressions outside of the parentheses.</dd>\n",
    "\t<dt><code>print(content)</code></dt>\n",
    "\t<dd>Displays <code>content</code> to the user. Can display multiple values on one line, when separated by <code>,</code>.</dd>\n",
    "\t<dt><code>input(prompt)</code></dt>\n",
    "\t<dd>Displays <code>prompt</code> to the user and asks them to provide input which can be saved to a variable.</dd>\n",
    "\t<dt><code>.strip()</code></dt>\n",
    "\t<dd>Removes excess whitespace from the beginning and end of a string.</dd>\n",
    "\t<dt><code>#</code></dt>\n",
    "\t<dd>Starts a comment, telling Python to ignore the rest of the line.</dd>\n",
    "</dl>"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "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.11.11"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
