{ "cells": [ { "cell_type": "markdown", "id": "comprehensive-requirement", "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": "extensive-engagement", "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": "civilian-ambassador", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", "id": "representative-month", "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": "important-harvey", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", "id": "advanced-blackberry", "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": null, "id": "solar-concrete", "metadata": {}, "outputs": [], "source": [ "# Fix this code below line 11! Don't change the rainbow variable!\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", "# ------------------------------------------------------\n", "# Start Fixing Below Here\n", "# You may encounter: NameError, SyntaxError, IndentationError, KeyError, TypeError, etc.\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", "while color in rainbow[\"colors\"]:\n", " if color is not \"Indigo\":\n", " print(color)\n", " else: \n", " print(\"What color is indigo?\")\n", "\n", "print(\"This rainbow is a double?\", rainbow[\"double\"])\n", "\n", "gold_amount = rainbow[\"number\"] + rainbow[\"gold\"]\n", "print(gold_amount)\n", "\n", "persons = 0\n", "split_gold = rainbow[\"gold\"] / persons\n", "print(split_gold)" ] }, { "cell_type": "markdown", "id": "innovative-wilson", "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": null, "id": "solved-python", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", "id": "attempted-announcement", "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": 1, "id": "agreed-acquisition", "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(\"my_characters\", \"w\")\n", " f.write(f\"{index+1}: {character}\\n\")\n", " f.close()" ] }, { "cell_type": "markdown", "id": "registered-consequence", "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": "markdown", "id": "based-spotlight", "metadata": {}, "source": [ "---\n", "## Last, But Not Least\n", "\n", "You're all done! Wow! Time for a well-earned break!\n", "\n", "![](https://media.giphy.com/media/SJsuD0XD2zNug/giphy.gif)" ] } ], "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 }