{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Topic 6: Practice problems (error handling and file basics)\n",
    "\n",
    "Asiman Data School, Module 1: Foundations of Python Programming. Week 3, Thursday.\n",
    "\n",
    "Fifteen problems in three levels, on the same ideas and at the same level as the lesson notebook.\n",
    "\n",
    "How to work\n",
    "\n",
    "- Write your code in the cell under each problem. The lines that are already in the cell read the input for you. Where a problem asks for a function, write it above those lines; until it is written, the cell stops with a NameError, and that is expected.\n",
    "- When the cell runs, a small box appears. Type the value there and press Enter. Several numbers on one line are typed with spaces between them.\n",
    "- Run your code with every row of the examples table and compare the output. Print exactly what the problem asks for.\n",
    "- A hint and one possible solution are under each problem. Open them only after you have tried.\n",
    "\n",
    "Tip: For every problem, ask: what can go wrong, and which error type is it? Then keep only the risky lines inside `try`."
   ],
   "id": "t06p-000"
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Easy"
   ],
   "id": "t06p-001"
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Problem 1. Double, or not a number\n",
    "\n",
    "Read a text from the user. Print the number doubled, or `not a number` if it is not a whole number.\n",
    "\n",
    "Examples\n",
    "\n",
    "| Input | Output |\n",
    "|---|---|\n",
    "| `21` | `42` |\n",
    "| `abc` | `not a number` |\n",
    "| `-4` | `-8` |\n",
    "| `3.5` | `not a number` |\n",
    "| `0` | `0` |"
   ],
   "id": "t06p-002"
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "asiman": {
     "typed": [
      "21"
     ]
    }
   },
   "outputs": [],
   "source": [
    "text = input()\n",
    "# Write your code here"
   ],
   "id": "t06p-003"
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<details>\n",
    "<summary><b>Show a hint</b></summary>\n",
    "\n",
    "Put `int(text)` inside `try`. The error type for a text that is not a whole number is `ValueError`.\n",
    "\n",
    "</details>\n",
    "\n",
    "<details>\n",
    "<summary><b>Show the solution</b></summary>\n",
    "\n",
    "```python\n",
    "text = input()\n",
    "try:\n",
    "    print(int(text) * 2)\n",
    "except ValueError:\n",
    "    print(\"not a number\")\n",
    "```\n",
    "\n",
    "**Test cases**\n",
    "\n",
    "| Input | Output |\n",
    "|---|---|\n",
    "| `21` | `42` |\n",
    "| `abc` | `not a number` |\n",
    "| `-4` | `-8` |\n",
    "| `3.5` | `not a number` |\n",
    "| `0` | `0` |\n",
    "\n",
    "</details>"
   ],
   "id": "t06p-004"
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Problem 2. A safe division\n",
    "\n",
    "Read two whole numbers from one line and print the first divided by the second, or `cannot divide by zero`.\n",
    "\n",
    "Examples\n",
    "\n",
    "| Input | Output |\n",
    "|---|---|\n",
    "| `10 2` | `5.0` |\n",
    "| `7 0` | `cannot divide by zero` |\n",
    "| `0 5` | `0.0` |\n",
    "| `-9 3` | `-3.0` |"
   ],
   "id": "t06p-005"
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "asiman": {
     "typed": [
      "10 2"
     ]
    }
   },
   "outputs": [],
   "source": [
    "a, b = map(int, input().split())\n",
    "# Write your code here"
   ],
   "id": "t06p-006"
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<details>\n",
    "<summary><b>Show a hint</b></summary>\n",
    "\n",
    "The division is the risky line. Its error type is `ZeroDivisionError`.\n",
    "\n",
    "</details>\n",
    "\n",
    "<details>\n",
    "<summary><b>Show the solution</b></summary>\n",
    "\n",
    "```python\n",
    "a, b = map(int, input().split())\n",
    "try:\n",
    "    print(a / b)\n",
    "except ZeroDivisionError:\n",
    "    print(\"cannot divide by zero\")\n",
    "```\n",
    "\n",
    "**Test cases**\n",
    "\n",
    "| Input | Output |\n",
    "|---|---|\n",
    "| `10 2` | `5.0` |\n",
    "| `7 0` | `cannot divide by zero` |\n",
    "| `0 5` | `0.0` |\n",
    "| `-9 3` | `-3.0` |\n",
    "\n",
    "</details>"
   ],
   "id": "t06p-007"
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Problem 3. A safe index\n",
    "\n",
    "Given `items = [\"a\", \"b\", \"c\"]`, read an index from the user and print the item at that index, or `no such index`.\n",
    "\n",
    "Examples\n",
    "\n",
    "| Input | Output |\n",
    "|---|---|\n",
    "| `0` | `a` |\n",
    "| `2` | `c` |\n",
    "| `5` | `no such index` |\n",
    "| `-1` | `c` |\n",
    "| `3` | `no such index` |"
   ],
   "id": "t06p-008"
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "asiman": {
     "typed": [
      "0"
     ]
    }
   },
   "outputs": [],
   "source": [
    "items = [\"a\", \"b\", \"c\"]\n",
    "index = int(input())\n",
    "# Write your code here"
   ],
   "id": "t06p-009"
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<details>\n",
    "<summary><b>Show a hint</b></summary>\n",
    "\n",
    "`items[index]` gives an `IndexError` when the index does not exist. A negative index such as -1 is valid.\n",
    "\n",
    "</details>\n",
    "\n",
    "<details>\n",
    "<summary><b>Show the solution</b></summary>\n",
    "\n",
    "```python\n",
    "items = [\"a\", \"b\", \"c\"]\n",
    "index = int(input())\n",
    "try:\n",
    "    print(items[index])\n",
    "except IndexError:\n",
    "    print(\"no such index\")\n",
    "```\n",
    "\n",
    "**Test cases**\n",
    "\n",
    "| Input | Output |\n",
    "|---|---|\n",
    "| `0` | `a` |\n",
    "| `2` | `c` |\n",
    "| `5` | `no such index` |\n",
    "| `-1` | `c` |\n",
    "| `3` | `no such index` |\n",
    "\n",
    "</details>"
   ],
   "id": "t06p-010"
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Problem 4. A safe lookup\n",
    "\n",
    "Given `prices = {\"apple\": 1.5, \"bread\": 0.8}`, read a product name and print its price, or `no such product`. Use `try/except`.\n",
    "\n",
    "Examples\n",
    "\n",
    "| Input | Output |\n",
    "|---|---|\n",
    "| `apple` | `1.5` |\n",
    "| `bread` | `0.8` |\n",
    "| `kiwi` | `no such product` |\n",
    "| `Apple` | `no such product` |"
   ],
   "id": "t06p-011"
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "asiman": {
     "typed": [
      "apple"
     ]
    }
   },
   "outputs": [],
   "source": [
    "prices = {\"apple\": 1.5, \"bread\": 0.8}\n",
    "product = input()\n",
    "# Write your code here"
   ],
   "id": "t06p-012"
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<details>\n",
    "<summary><b>Show a hint</b></summary>\n",
    "\n",
    "`prices[product]` gives a `KeyError` when the key is missing.\n",
    "\n",
    "</details>\n",
    "\n",
    "<details>\n",
    "<summary><b>Show the solution</b></summary>\n",
    "\n",
    "```python\n",
    "prices = {\"apple\": 1.5, \"bread\": 0.8}\n",
    "product = input()\n",
    "try:\n",
    "    print(prices[product])\n",
    "except KeyError:\n",
    "    print(\"no such product\")\n",
    "```\n",
    "\n",
    "**Test cases**\n",
    "\n",
    "| Input | Output |\n",
    "|---|---|\n",
    "| `apple` | `1.5` |\n",
    "| `bread` | `0.8` |\n",
    "| `kiwi` | `no such product` |\n",
    "| `Apple` | `no such product` |\n",
    "\n",
    "</details>"
   ],
   "id": "t06p-013"
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Problem 5. What kind of number?\n",
    "\n",
    "Read a text and print `whole number`, `decimal number` or `not a number`.\n",
    "\n",
    "Examples\n",
    "\n",
    "| Input | Output |\n",
    "|---|---|\n",
    "| `42` | `whole number` |\n",
    "| `3.5` | `decimal number` |\n",
    "| `abc` | `not a number` |\n",
    "| `-7` | `whole number` |\n",
    "| `2.` | `decimal number` |"
   ],
   "id": "t06p-014"
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "asiman": {
     "typed": [
      "42"
     ]
    }
   },
   "outputs": [],
   "source": [
    "text = input()\n",
    "# Write your code here"
   ],
   "id": "t06p-015"
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<details>\n",
    "<summary><b>Show a hint</b></summary>\n",
    "\n",
    "Try `int(text)` first. In its `except` block, try `float(text)` with a second `try/except`.\n",
    "\n",
    "</details>\n",
    "\n",
    "<details>\n",
    "<summary><b>Show the solution</b></summary>\n",
    "\n",
    "```python\n",
    "text = input()\n",
    "try:\n",
    "    int(text)\n",
    "    print(\"whole number\")\n",
    "except ValueError:\n",
    "    try:\n",
    "        float(text)\n",
    "        print(\"decimal number\")\n",
    "    except ValueError:\n",
    "        print(\"not a number\")\n",
    "```\n",
    "\n",
    "**Test cases**\n",
    "\n",
    "| Input | Output |\n",
    "|---|---|\n",
    "| `42` | `whole number` |\n",
    "| `3.5` | `decimal number` |\n",
    "| `abc` | `not a number` |\n",
    "| `-7` | `whole number` |\n",
    "| `2.` | `decimal number` |\n",
    "\n",
    "</details>"
   ],
   "id": "t06p-016"
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Medium"
   ],
   "id": "t06p-017"
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Problem 6. Sum of the valid values\n",
    "\n",
    "Read a line of values separated by spaces. Print the sum of the values that are whole numbers and how many values were skipped, on one line with a space between them.\n",
    "\n",
    "Examples\n",
    "\n",
    "| Input | Output |\n",
    "|---|---|\n",
    "| `10 abc 30 x 5` | `45 2` |\n",
    "| `1 2 3` | `6 0` |\n",
    "| `a b` | `0 2` |\n",
    "| `7.5 2` | `2 1` |\n",
    "| `-4 four` | `-4 1` |"
   ],
   "id": "t06p-018"
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "asiman": {
     "typed": [
      "10 abc 30 x 5"
     ]
    }
   },
   "outputs": [],
   "source": [
    "values = input().split()\n",
    "# Write your code here"
   ],
   "id": "t06p-019"
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<details>\n",
    "<summary><b>Show a hint</b></summary>\n",
    "\n",
    "Two variables, `total` and `skipped`. The `try` goes inside the loop: add in `try`, count the skipped value in `except`.\n",
    "\n",
    "</details>\n",
    "\n",
    "<details>\n",
    "<summary><b>Show the solution</b></summary>\n",
    "\n",
    "```python\n",
    "values = input().split()\n",
    "total = 0\n",
    "skipped = 0\n",
    "for value in values:\n",
    "    try:\n",
    "        total = total + int(value)\n",
    "    except ValueError:\n",
    "        skipped = skipped + 1\n",
    "print(total, skipped)\n",
    "```\n",
    "\n",
    "**Test cases**\n",
    "\n",
    "| Input | Output |\n",
    "|---|---|\n",
    "| `10 abc 30 x 5` | `45 2` |\n",
    "| `1 2 3` | `6 0` |\n",
    "| `a b` | `0 2` |\n",
    "| `7.5 2` | `2 1` |\n",
    "| `-4 four` | `-4 1` |\n",
    "\n",
    "</details>"
   ],
   "id": "t06p-020"
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Problem 7. A division function\n",
    "\n",
    "Write a function `safe_divide(a, b)` that returns the result, or `None` if `b` is zero.\n",
    "\n",
    "Examples\n",
    "\n",
    "| Input | Output |\n",
    "|---|---|\n",
    "| `10 2` | `5.0` |\n",
    "| `10 0` | `None` |\n",
    "| `0 3` | `0.0` |\n",
    "| `-6 4` | `-1.5` |"
   ],
   "id": "t06p-021"
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "asiman": {
     "typed": [
      "10 2"
     ]
    }
   },
   "outputs": [],
   "source": [
    "# Write your function here\n",
    "\n",
    "\n",
    "a, b = map(int, input().split())\n",
    "print(safe_divide(a, b))"
   ],
   "id": "t06p-022"
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<details>\n",
    "<summary><b>Show a hint</b></summary>\n",
    "\n",
    "`return a / b` inside `try`, and `return None` inside `except ZeroDivisionError:`.\n",
    "\n",
    "</details>\n",
    "\n",
    "<details>\n",
    "<summary><b>Show the solution</b></summary>\n",
    "\n",
    "```python\n",
    "def safe_divide(a, b):\n",
    "    try:\n",
    "        return a / b\n",
    "    except ZeroDivisionError:\n",
    "        return None\n",
    "\n",
    "a, b = map(int, input().split())\n",
    "print(safe_divide(a, b))\n",
    "```\n",
    "\n",
    "**Test cases**\n",
    "\n",
    "| Input | Output |\n",
    "|---|---|\n",
    "| `10 2` | `5.0` |\n",
    "| `10 0` | `None` |\n",
    "| `0 3` | `0.0` |\n",
    "| `-6 4` | `-1.5` |\n",
    "\n",
    "</details>"
   ],
   "id": "t06p-023"
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Problem 8. A conversion function\n",
    "\n",
    "Write a function `safe_int(text, default)` that returns the text as a whole number, or `default` if it cannot be converted.\n",
    "\n",
    "Examples\n",
    "\n",
    "| Input | Output |\n",
    "|---|---|\n",
    "| `42` | `42` |\n",
    "| `abc` | `-1` |\n",
    "| `-8` | `-8` |\n",
    "| `3.5` | `-1` |\n",
    "| `007` | `7` |"
   ],
   "id": "t06p-024"
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "asiman": {
     "typed": [
      "42"
     ]
    }
   },
   "outputs": [],
   "source": [
    "# Write your function here\n",
    "\n",
    "\n",
    "text = input()\n",
    "print(safe_int(text, -1))"
   ],
   "id": "t06p-025"
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<details>\n",
    "<summary><b>Show a hint</b></summary>\n",
    "\n",
    "`return int(text)` inside `try`, and `return default` inside `except ValueError:`.\n",
    "\n",
    "</details>\n",
    "\n",
    "<details>\n",
    "<summary><b>Show the solution</b></summary>\n",
    "\n",
    "```python\n",
    "def safe_int(text, default):\n",
    "    try:\n",
    "        return int(text)\n",
    "    except ValueError:\n",
    "        return default\n",
    "\n",
    "text = input()\n",
    "print(safe_int(text, -1))\n",
    "```\n",
    "\n",
    "**Test cases**\n",
    "\n",
    "| Input | Output |\n",
    "|---|---|\n",
    "| `42` | `42` |\n",
    "| `abc` | `-1` |\n",
    "| `-8` | `-8` |\n",
    "| `3.5` | `-1` |\n",
    "| `007` | `7` |\n",
    "\n",
    "</details>"
   ],
   "id": "t06p-026"
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Problem 9. Ask until it is valid\n",
    "\n",
    "Keep asking the user for a whole number from 1 to 5 until the input is valid. Then print `ok` and the number. Print nothing for the wrong answers.\n",
    "\n",
    "Examples\n",
    "\n",
    "| Input | Output |\n",
    "|---|---|\n",
    "| `3` | `ok 3` |\n",
    "| `abc, 9, 4` | `ok 4` |\n",
    "| `0, 6, 1` | `ok 1` |\n",
    "| `2.5, 5` | `ok 5` |"
   ],
   "id": "t06p-027"
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "asiman": {
     "typed": [
      "3"
     ]
    }
   },
   "outputs": [],
   "source": [
    "# Write your code here"
   ],
   "id": "t06p-028"
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<details>\n",
    "<summary><b>Show a hint</b></summary>\n",
    "\n",
    "`while True:` with `int(input())` inside `try`. In `except ValueError:` write `continue`. After the `try`, `break` when the number is from 1 to 5.\n",
    "\n",
    "</details>\n",
    "\n",
    "<details>\n",
    "<summary><b>Show the solution</b></summary>\n",
    "\n",
    "```python\n",
    "while True:\n",
    "    try:\n",
    "        number = int(input())\n",
    "    except ValueError:\n",
    "        continue\n",
    "    if 1 <= number <= 5:\n",
    "        break\n",
    "print(\"ok\", number)\n",
    "```\n",
    "\n",
    "**Test cases**\n",
    "\n",
    "| Input | Output |\n",
    "|---|---|\n",
    "| `3` | `ok 3` |\n",
    "| `abc, 9, 4` | `ok 4` |\n",
    "| `0, 6, 1` | `ok 1` |\n",
    "| `2.5, 5` | `ok 5` |\n",
    "\n",
    "</details>"
   ],
   "id": "t06p-029"
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Problem 10. A character by its index\n",
    "\n",
    "Read a text and an index (two lines). Print the character at that index. Print `not a number` if the index is not a whole number, and `no such index` if it is out of range.\n",
    "\n",
    "Examples\n",
    "\n",
    "| Input | Output |\n",
    "|---|---|\n",
    "| `python, 0` | `p` |\n",
    "| `python, 10` | `no such index` |\n",
    "| `python, x` | `not a number` |\n",
    "| `hi, -1` | `i` |\n",
    "| `a, 1` | `no such index` |"
   ],
   "id": "t06p-030"
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "asiman": {
     "typed": [
      "python",
      "0"
     ]
    }
   },
   "outputs": [],
   "source": [
    "text = input()\n",
    "# Write your code here"
   ],
   "id": "t06p-031"
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<details>\n",
    "<summary><b>Show a hint</b></summary>\n",
    "\n",
    "Two things can go wrong, so two `except` blocks: `ValueError` for the conversion and `IndexError` for the index.\n",
    "\n",
    "</details>\n",
    "\n",
    "<details>\n",
    "<summary><b>Show the solution</b></summary>\n",
    "\n",
    "```python\n",
    "text = input()\n",
    "try:\n",
    "    index = int(input())\n",
    "    print(text[index])\n",
    "except ValueError:\n",
    "    print(\"not a number\")\n",
    "except IndexError:\n",
    "    print(\"no such index\")\n",
    "```\n",
    "\n",
    "**Test cases**\n",
    "\n",
    "| Input | Output |\n",
    "|---|---|\n",
    "| `python, 0` | `p` |\n",
    "| `python, 10` | `no such index` |\n",
    "| `python, x` | `not a number` |\n",
    "| `hi, -1` | `i` |\n",
    "| `a, 1` | `no such index` |\n",
    "\n",
    "</details>"
   ],
   "id": "t06p-032"
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Medium-Hard"
   ],
   "id": "t06p-033"
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Problem 11. Check the age\n",
    "\n",
    "Write a function `check_age(age)` that raises a `ValueError` with a message if the age is below 0 or above 120, and returns the age otherwise. Read an age and print `ok`, or the message. The message is `age must be between 0 and 120`.\n",
    "\n",
    "Examples\n",
    "\n",
    "| Input | Output |\n",
    "|---|---|\n",
    "| `20` | `ok` |\n",
    "| `-1` | `age must be between 0 and 120` |\n",
    "| `121` | `age must be between 0 and 120` |\n",
    "| `0` | `ok` |\n",
    "| `120` | `ok` |"
   ],
   "id": "t06p-034"
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "asiman": {
     "typed": [
      "20"
     ]
    }
   },
   "outputs": [],
   "source": [
    "# Write your function here\n",
    "\n",
    "\n",
    "age = int(input())\n",
    "# Call check_age here and print ok, or the message"
   ],
   "id": "t06p-035"
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<details>\n",
    "<summary><b>Show a hint</b></summary>\n",
    "\n",
    "Inside the function: `if age < 0 or age > 120:` then `raise ValueError(\"age must be between 0 and 120\")`. Outside: call it in `try`, and print the error in `except ValueError as error:`.\n",
    "\n",
    "</details>\n",
    "\n",
    "<details>\n",
    "<summary><b>Show the solution</b></summary>\n",
    "\n",
    "```python\n",
    "def check_age(age):\n",
    "    if age < 0 or age > 120:\n",
    "        raise ValueError(\"age must be between 0 and 120\")\n",
    "    return age\n",
    "\n",
    "age = int(input())\n",
    "try:\n",
    "    check_age(age)\n",
    "    print(\"ok\")\n",
    "except ValueError as error:\n",
    "    print(error)\n",
    "```\n",
    "\n",
    "**Test cases**\n",
    "\n",
    "| Input | Output |\n",
    "|---|---|\n",
    "| `20` | `ok` |\n",
    "| `-1` | `age must be between 0 and 120` |\n",
    "| `121` | `age must be between 0 and 120` |\n",
    "| `0` | `ok` |\n",
    "| `120` | `ok` |\n",
    "\n",
    "</details>"
   ],
   "id": "t06p-036"
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Problem 12. Records with mistakes\n",
    "\n",
    "Read a line of records like `Aylin:85 Murad:x Leyla:78`. Build a dictionary of the valid ones. Print the dictionary and how many records were bad, on one line with a space between them.\n",
    "\n",
    "Examples\n",
    "\n",
    "| Input | Output |\n",
    "|---|---|\n",
    "| `Aylin:85 Murad:x Leyla:78` | `{'Aylin': 85, 'Leyla': 78} 1` |\n",
    "| `A:1` | `{'A': 1} 0` |\n",
    "| `A B:2` | `{'B': 2} 1` |\n",
    "| `x:y z:w` | `{} 2` |\n",
    "| `K:10 L:20 M` | `{'K': 10, 'L': 20} 1` |"
   ],
   "id": "t06p-037"
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "asiman": {
     "typed": [
      "Aylin:85 Murad:x Leyla:78"
     ]
    }
   },
   "outputs": [],
   "source": [
    "records = input().split()\n",
    "# Write your code here"
   ],
   "id": "t06p-038"
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<details>\n",
    "<summary><b>Show a hint</b></summary>\n",
    "\n",
    "For every record, `name, grade = record.split(\":\")` and `int(grade)` go inside `try`. Both problems (no colon, a grade that is not a number) are a `ValueError`.\n",
    "\n",
    "</details>\n",
    "\n",
    "<details>\n",
    "<summary><b>Show the solution</b></summary>\n",
    "\n",
    "```python\n",
    "records = input().split()\n",
    "grades = {}\n",
    "bad = 0\n",
    "for record in records:\n",
    "    try:\n",
    "        name, grade = record.split(\":\")\n",
    "        grades[name] = int(grade)\n",
    "    except ValueError:\n",
    "        bad = bad + 1\n",
    "print(grades, bad)\n",
    "```\n",
    "\n",
    "**Test cases**\n",
    "\n",
    "| Input | Output |\n",
    "|---|---|\n",
    "| `Aylin:85 Murad:x Leyla:78` | `{'Aylin': 85, 'Leyla': 78} 1` |\n",
    "| `A:1` | `{'A': 1} 0` |\n",
    "| `A B:2` | `{'B': 2} 1` |\n",
    "| `x:y z:w` | `{} 2` |\n",
    "| `K:10 L:20 M` | `{'K': 10, 'L': 20} 1` |\n",
    "\n",
    "</details>"
   ],
   "id": "t06p-039"
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Problem 13. Minutes since midnight\n",
    "\n",
    "Write a function `parse_time(text)` that takes a text like `14:30` and returns the minutes since midnight. It raises a `ValueError` if the text is not in that form or the numbers are out of range (hours 0 to 23, minutes 0 to 59). The last lines print the result, or `invalid time`.\n",
    "\n",
    "Examples\n",
    "\n",
    "| Input | Output |\n",
    "|---|---|\n",
    "| `14:30` | `870` |\n",
    "| `00:00` | `0` |\n",
    "| `23:59` | `1439` |\n",
    "| `24:00` | `invalid time` |\n",
    "| `12:60` | `invalid time` |\n",
    "| `noon` | `invalid time` |\n",
    "| `7:5` | `425` |\n",
    "| `12-30` | `invalid time` |"
   ],
   "id": "t06p-040"
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "asiman": {
     "typed": [
      "14:30"
     ]
    }
   },
   "outputs": [],
   "source": [
    "# Write your function here\n",
    "\n",
    "\n",
    "text = input()\n",
    "try:\n",
    "    print(parse_time(text))\n",
    "except ValueError:\n",
    "    print(\"invalid time\")"
   ],
   "id": "t06p-041"
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<details>\n",
    "<summary><b>Show a hint</b></summary>\n",
    "\n",
    "Split the text at `\":\"` into two variables and convert both with `int()`. A wrong form already gives a `ValueError` by itself. For numbers out of range, `raise ValueError(...)` yourself.\n",
    "\n",
    "</details>\n",
    "\n",
    "<details>\n",
    "<summary><b>Show the solution</b></summary>\n",
    "\n",
    "```python\n",
    "def parse_time(text):\n",
    "    hours, minutes = text.split(\":\")\n",
    "    hours = int(hours)\n",
    "    minutes = int(minutes)\n",
    "    if hours < 0 or hours > 23 or minutes < 0 or minutes > 59:\n",
    "        raise ValueError(\"out of range\")\n",
    "    return hours * 60 + minutes\n",
    "\n",
    "text = input()\n",
    "try:\n",
    "    print(parse_time(text))\n",
    "except ValueError:\n",
    "    print(\"invalid time\")\n",
    "```\n",
    "\n",
    "**Test cases**\n",
    "\n",
    "| Input | Output |\n",
    "|---|---|\n",
    "| `14:30` | `870` |\n",
    "| `00:00` | `0` |\n",
    "| `23:59` | `1439` |\n",
    "| `24:00` | `invalid time` |\n",
    "| `12:60` | `invalid time` |\n",
    "| `noon` | `invalid time` |\n",
    "| `7:5` | `425` |\n",
    "| `12-30` | `invalid time` |\n",
    "\n",
    "</details>"
   ],
   "id": "t06p-042"
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Problem 14. Good values and bad values\n",
    "\n",
    "Write a function `to_numbers(values)` that returns two lists: the values that could be converted to whole numbers, and the values that could not.\n",
    "\n",
    "Examples\n",
    "\n",
    "| Input | Output |\n",
    "|---|---|\n",
    "| `10 abc 30 x` | `([10, 30], ['abc', 'x'])` |\n",
    "| `1 2` | `([1, 2], [])` |\n",
    "| `a` | `([], ['a'])` |\n",
    "| `-5 5.5` | `([-5], ['5.5'])` |"
   ],
   "id": "t06p-043"
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "asiman": {
     "typed": [
      "10 abc 30 x"
     ]
    }
   },
   "outputs": [],
   "source": [
    "# Write your function here\n",
    "\n",
    "\n",
    "values = input().split()\n",
    "print(to_numbers(values))"
   ],
   "id": "t06p-044"
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<details>\n",
    "<summary><b>Show a hint</b></summary>\n",
    "\n",
    "Two empty lists. In the loop, `good.append(int(value))` inside `try` and `bad.append(value)` inside `except ValueError:`. Return both: `return good, bad`.\n",
    "\n",
    "</details>\n",
    "\n",
    "<details>\n",
    "<summary><b>Show the solution</b></summary>\n",
    "\n",
    "```python\n",
    "def to_numbers(values):\n",
    "    good = []\n",
    "    bad = []\n",
    "    for value in values:\n",
    "        try:\n",
    "            good.append(int(value))\n",
    "        except ValueError:\n",
    "            bad.append(value)\n",
    "    return good, bad\n",
    "\n",
    "values = input().split()\n",
    "print(to_numbers(values))\n",
    "```\n",
    "\n",
    "**Test cases**\n",
    "\n",
    "| Input | Output |\n",
    "|---|---|\n",
    "| `10 abc 30 x` | `([10, 30], ['abc', 'x'])` |\n",
    "| `1 2` | `([1, 2], [])` |\n",
    "| `a` | `([], ['a'])` |\n",
    "| `-5 5.5` | `([-5], ['5.5'])` |\n",
    "\n",
    "</details>"
   ],
   "id": "t06p-045"
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Problem 15. A file with bad lines\n",
    "\n",
    "A file has one value per line, and some lines are not numbers. Print the sum of the valid numbers and how many lines were skipped, on one line with a space between them. The first lines of the editor create the file."
   ],
   "id": "t06p-046"
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "with open(\"values.txt\", \"w\") as file:\n",
    "    file.write(\"10\\nabc\\n30\\n\\n5\\n\")\n",
    "\n",
    "# Write your code here"
   ],
   "id": "t06p-047"
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<details>\n",
    "<summary><b>Show a hint</b></summary>\n",
    "\n",
    "Open the file for reading and loop over its lines. `int(line)` goes inside `try`. An empty line is a `ValueError` too.\n",
    "\n",
    "</details>\n",
    "\n",
    "<details>\n",
    "<summary><b>Show the solution</b></summary>\n",
    "\n",
    "```python\n",
    "with open(\"values.txt\", \"w\") as file:\n",
    "    file.write(\"10\\nabc\\n30\\n\\n5\\n\")\n",
    "\n",
    "total = 0\n",
    "skipped = 0\n",
    "with open(\"values.txt\") as file:\n",
    "    for line in file:\n",
    "        try:\n",
    "            total = total + int(line)\n",
    "        except ValueError:\n",
    "            skipped = skipped + 1\n",
    "print(total, skipped)\n",
    "```\n",
    "\n",
    "**Output**\n",
    "\n",
    "```\n",
    "45 2\n",
    "```\n",
    "\n",
    "</details>"
   ],
   "id": "t06p-048"
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "When you have finished, compare your solutions with the hidden ones. A different solution that passes every example is just as good."
   ],
   "id": "t06p-049"
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "name": "python"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
