{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "hispanic-project",
   "metadata": {},
   "source": [
    "# Advanced Python: Practice Problems\n",
    "\n",
    "In this homework, you're going to write code for a few problems. Like before, we'll be practicing new material, but you may need to break out your skills from previous units.\n",
    "\n",
    "You will practice these programming concepts we've covered in class:\n",
    "* Using `itertools` and `random` functions.\n",
    "* Type conversion and more advanced variable usage.\n",
    "* Calling an API to get data using `requests`.\n",
    "* Debugging code that doesn't work using `print` statements.\n",
    "* Scripting and file I/O.\n",
    "* Using list comprehensions to simplify list creation."
   ]
  },
  {
   "cell_type": "markdown",
   "id": "informed-transcription",
   "metadata": {},
   "source": [
    "---\n",
    "## Problem 1:\n",
    "\n",
    "### Skills you're practicing: Generating random numbers and type conversion.\n",
    "\n",
    "Make your own version of the `Guess a Number` game. Generate a random integer and store it in a variable called `answer`. Print a statement asking the user to guess a number.\n",
    "\n",
    "If the user's guess is:\n",
    "* Higher than the `answer`: print `That is too high!`.\n",
    "* Lower than the `answer`: print `That is too low!`.\n",
    "* Exactly the `answer`: print `That's it! You win!`.\n",
    "\n",
    "Your program should keep prompting the user until they enter the correct answer.\n",
    "\n",
    "#### Example Output\n",
    "```\n",
    "I'm thinking of a number between 1 and 10.\n",
    "Please guess what it is:\n",
    "> 4\n",
    "That is too low!\n",
    "> 8\n",
    "That is too high!\n",
    "> 6\n",
    "That's it! You win!\n",
    "```\n",
    "\n",
    "> **Hint 1:** User input comes to you as a string. How can you make it into an integer so you can properly compare the user's guess with the `answer` (which is an integer)?\n",
    "\n",
    "> **Hint 2:** You can set a variable to `False` or `True`."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "blocked-lottery",
   "metadata": {},
   "outputs": [],
   "source": [
    "import random\n",
    "\n",
    "answer = random.randint(1, 10)\n",
    "guessed = False\n",
    "\n",
    "print(\"I'm thinking of a number between 1 and 10.\")\n",
    "\n",
    "while not guessed:\n",
    "    current_guess = int(input(\"Please guess what it is:\"))\n",
    "\n",
    "    if current_guess == answer:\n",
    "        print(\"That's it! You win!\")\n",
    "        guessed = True\n",
    "    elif current_guess < answer:\n",
    "        print(\"That is too low!\")\n",
    "    elif current_guess > answer:\n",
    "        print(\"That is too high!\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "static-tuition",
   "metadata": {},
   "source": [
    "---\n",
    "## Problem 2: D'oh!\n",
    "\n",
    "### Skills you're practicing: Using `try`/`except` statements and error handling.\n",
    "\n",
    "Bart Simpson has gotten ahold of your program from Problem 1 and started entering a bunch of values that are NOT numbers! How do you handle it?\n",
    "\n",
    "Modify your code from Problem 1 to print `D'oh! That is NOT a number, Bart!` if the user doesn't enter an integer.\n",
    "\n",
    "#### Example Output\n",
    "```\n",
    "I'm thinking of a number between 1 and 10.\n",
    "Please guess what it is:\n",
    "> Eat my shorts!\n",
    "D'oh! That is NOT a number, Bart!\n",
    "Please guess what it is:\n",
    "> Ay, caramba!\n",
    "D'oh! That is NOT a number, Bart!\n",
    "Please guess what it is:\n",
    "> 5\n",
    "That is too low!\n",
    "Please guess what it is:\n",
    "> 8\n",
    "That's it! You win!\n",
    "```\n",
    "\n",
    "> **Hint:** The `continue` keyword can be called in a loop and means \"skip the rest of this loop iteration.\" It may be useful to call this in an `except` clause."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "divine-departure",
   "metadata": {},
   "outputs": [],
   "source": [
    "import random\n",
    "\n",
    "answer = random.randint(1, 10)\n",
    "guessed = False\n",
    "\n",
    "print(\"I'm thinking of a number between 1 and 10.\")\n",
    "\n",
    "while not guessed:\n",
    "    current_guess = input(\"Please guess what it is:\")\n",
    "\n",
    "    try:\n",
    "        current_guess = int(current_guess)\n",
    "    except:\n",
    "        print(\"D'oh! That is NOT a number, Bart!\")\n",
    "        continue # This skips the rest of this loop iteration.\n",
    "\n",
    "    if current_guess == answer:\n",
    "        print(\"That's it! You win!\")\n",
    "        guessed = True\n",
    "    elif current_guess < answer:\n",
    "        print(\"That is too low!\")\n",
    "    elif current_guess > answer:\n",
    "        print(\"That is too high!\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "floating-victor",
   "metadata": {},
   "source": [
    "---\n",
    "## Problem 3:\n",
    "\n",
    "### Skill you're practicing: Debugging built-in errors.\n",
    "\n",
    "Fix the errors in the code. Don't change the `rainbow`'s contents.\n",
    "\n",
    "#### Expected Output\n",
    "```\n",
    "The rainbow's name is Roy G. Biv\n",
    "I'll be taking that gold!\n",
    "red\n",
    "orange\n",
    "yellow\n",
    "green\n",
    "blue\n",
    "What color is indigo?\n",
    "violet\n",
    "102\n",
    "100.0\n",
    "```\n",
    "\n",
    "> **Hint:** You may encounter: `NameError`, `SyntaxError`, `IndentationError`, `KeyError`, `TypeError`, etc. Refer to the class notes for common causes of these errors."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "particular-behavior",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "The rainbow's name is Roy G. Biv\n",
      "I'll be taking that gold!\n",
      "red\n",
      "orange\n",
      "yellow\n",
      "green\n",
      "blue\n",
      "What color is indigo?\n",
      "violet\n",
      "102\n",
      "100.0\n"
     ]
    }
   ],
   "source": [
    "# Fixed Code (You may have some small variations!)\n",
    "rainbow = {\n",
    "  \"name\": \"Roy G. Biv\",\n",
    "  \"colors\": [\"red\", \"orange\", \"yellow\", \"green\", \"blue\", \"indigo\", \"violet\"],\n",
    "  \"end\": \"Pot of gold!\",\n",
    "  \"number\": \"double\",\n",
    "  \"count\": 2,\n",
    "  \"gold\": 100\n",
    "}\n",
    "\n",
    "print(\"The rainbow's name is\", rainbow[\"name\"])\n",
    "\n",
    "if rainbow[\"end\"] == \"Pot of gold!\":\n",
    "    print(\"I'll be taking that gold!\")\n",
    "else:\n",
    "    print(\"No money for me!\")\n",
    "\n",
    "for color in rainbow[\"colors\"]:\n",
    "    if color is not \"indigo\":\n",
    "        print(color)\n",
    "    else:\n",
    "        print(\"What color is indigo?\")\n",
    "\n",
    "gold_amount = rainbow[\"count\"] + rainbow[\"gold\"]\n",
    "print(gold_amount)\n",
    "\n",
    "persons = 1\n",
    "split_gold = rainbow[\"gold\"] / persons\n",
    "print(split_gold)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "perceived-puzzle",
   "metadata": {},
   "source": [
    "---\n",
    "## Problem 4: Three Commas\n",
    "\n",
    "### Skill you're practicing: Formatting numbers.\n",
    "\n",
    "Russ is a billionaire and he'd like to count his wealth in the number of commas it contains. For example, someone who is a millionaire would have only two commas (1,000,000). Russ, however, has three commas and would like to see those commas displayed. Help Russ format his integer so that commas appear when it is printed out. Make sure your program will work for any value of `wealth`.\n",
    "\n",
    "#### Example Code\n",
    "```python\n",
    "wealth = 1000000000\n",
    "```\n",
    "\n",
    "#### Example Output\n",
    "```\n",
    "1,000,000,000\n",
    "```\n",
    "\n",
    "> **Hint:** Look up `format` if you've forgotten how to use it!"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "indonesian-threat",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1,000,000,000\n"
     ]
    }
   ],
   "source": [
    "# We start out with an integer.\n",
    "wealth = 1000000000\n",
    "\n",
    "# We convert it to a string formatted the way we want.\n",
    "formatted = format(wealth, ',d')\n",
    "\n",
    "# Print it out!\n",
    "print(formatted)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "burning-relationship",
   "metadata": {},
   "source": [
    "---\n",
    "## Problem 5: My Favorite TV Characters\n",
    "\n",
    "### Skills you're practicing: Scripting and debugging.\n",
    "\n",
    "I'm trying to write a list of favorite TV show characters to a file called `my_characters`. I'm nearly there, but instead of printing the whole list, I'm just getting the last line. Help me out: Can you get the whole list to print to a file?\n",
    "\n",
    "#### My Code"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "limiting-withdrawal",
   "metadata": {},
   "outputs": [],
   "source": [
    "tv_characters = [\"Will Byers\", \"Tyrion Lannister\", \"Oliver Queen\", \"Jean Luc Picard\", \"Malcom Reynolds\", \"The Doctor\", \"Sam Winchester\", \"Sherlock Holmes\"]\n",
    "\n",
    "# Write out my character list to a file called \"text\"\n",
    "for index, character in enumerate(tv_characters):\n",
    "    f = open(\"text\", \"w\")\n",
    "    f.write(f\"{index+1}: {character}\\n\")\n",
    "    f.close()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "asian-resource",
   "metadata": {},
   "source": [
    "#### Desired Output\n",
    "```\n",
    "1: Will Byers\n",
    "2: Tyrion Lannister\n",
    "3: Oliver Queen\n",
    "4: Jean-Luc Picard\n",
    "5: Malcolm Reynolds\n",
    "6: The Doctor\n",
    "7: Sam Winchester\n",
    "8: Sherlock Holmes\n",
    "```\n",
    "\n",
    "#### Actual Output\n",
    "```\n",
    "8: Sherlock Holmes\n",
    "```\n",
    "\n",
    "> **Hint:** Remember the [spreadsheet](https://www.dropbox.com/s/cqsxfws52gulkyx/drawing.pdf) we looked at earlier? See if it can help!"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "postal-netherlands",
   "metadata": {},
   "outputs": [],
   "source": [
    "tv_characters = [\"Will Byers\", \"Tyrion Lannister\", \"Oliver Queen\", \"Jean-Luc Picard\", \"Malcolm Reynolds\", \"The Doctor\", \"Sam Winchester\", \"Sherlock Holmes\"]\n",
    "\n",
    "# Open the file outside of the loop.\n",
    "f = open(\"my_characters\", \"w\")\n",
    "\n",
    "for index, character in enumerate(tv_characters):\n",
    "    f.write(f\"{index+1}: {character}\\n\")\n",
    "\n",
    "# This happens outside the loop, too.\n",
    "f.close()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "fleet-fitness",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "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.7.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
