{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# [CptS 111 Introduction to Algorithmic Problem Solving](https://github.com/gsprint23/cpts111)\n", "[Washington State University](https://wsu.edu)\n", "\n", "[Gina Sprint](http://eecs.wsu.edu/~gsprint/)\n", "## PA2 Functions (75 pts)\n", "\n", "### Learner Objectives\n", "At the conclusion of this programming assignment, participants should be able to:\n", "* Call functions\n", "* Define functions\n", "* Import modules and use functions from imported modules\n", "\n", "### Prerequisites\n", "Before starting this programming assignment, participants should be able to:\n", "* Use variables, `print()` statements, and collect input from the user with `input()`\n", "* Convert values from one type to another (type casting)\n", "* Apply basic arithmetic\n", "\n", "### Acknowledgments\n", "Content used in this assignment is based upon information in the following sources:\n", "* None to report" ] }, { "cell_type": "markdown", "metadata": { "collapsed": true }, "source": [ "### Overview and Requirements\n", "Write a program (night_out.py) that simulates a night out on the town! The night out on the town is going to include that following activities (at a minimum):\n", "1. Dinner\n", "1. Concert\n", "\n", "Note: You can choose something other than a concert if you would like!\n", "\n", "The program will keep track of the amount of money spent for each activity (and associated car parking for the activity), including tax and tip where appropriate!\n", "\n", "#### Program Details\n", "Write a program that prompts the user to enter the following information:\n", "1. State sales tax (percent)\n", "1. Price to park for dinner\n", "1. Price of meal *before tax*\n", "1. Dinner tip (percent)\n", "1. Price to park for the concert\n", "1. Price of the concert *before tax*\n", "\n", "The output of the program consists of the following:\n", "1. Total money spent for each activity (dinner tab and concert)\n", "1. Total money spent parking the user's car for the activities (dinner parking + concert parking)\n", "1. Total money spent for the night out\n", "\n", "Every money amount displayed to the user should have a dollar sign and be rounded to two decimal places.\n", "\n", "##### Calculations\n", "1. Dinner\n", " * The total tab for dinner includes the meal, tax, and the tip. The tip will be calculated based on the combined meal and tax total. \n", " * The total tab for dinner should be **rounded *up* to nearest dollar**:\n", " $$tab = \\lceil meal + tax + tip \\rceil$$\n", " * Use the `ceil()` function from the `math` module to do this! \n", " * See `round_up_to_nearest_dollar()` below.\n", "2. Concert\n", " * The total cost for the concert includes the concert tickets and the tax.\n", "3. Parking\n", " * The total cost for parking includes the parking for dinner and the parking for the concert." ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "6\n" ] } ], "source": [ "# example use of ceil\n", "import math\n", "print(math.ceil(5.2))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### Functions to Define\n", "For this program, define the following functions:\n", "1. `display_instructions()`: Accepts no arguments. Prints instructions describing the use of the program to the user.\n", "1. `get_tax_percent()`: Accepts no arguments. Prompts the user for the state sales tax as a percent. Returns the sales tax percent.\n", "1. `get_tip_percent()`: Accepts no arguments. Prompts the user for what percentage of the bill (meal price + tax) they would like to tip. Returns the tip percent.\n", "1. `get_price(activity_label)`: Accepts 1 argument via the parameter `activity_label`, a string representing the price (before tax) to prompt the user for. Prompts the user to enter the price for `activity_label`. Returns a string representing the city.\n", " * Example: `get_price(\"dinner\")` prompts the user with the string: \"How much does dinner cost before tax?\"\n", "1. `calculate_tax(price, tax_percent)`: Accepts 2 arguments via the parameters `price`, a float representing the cost of the activity and `tax_percent`, a float representing the sales tax percent. Computes the tax for `price` and returns the result as a floating point number.\n", "1. `calculate_tip(price, tip_percent)`: Accepts 2 arguments via the parameters `price`, a float representing the cost of the activity and `tip_percent`, a float representing the percentage the user would like to tip. Computes the tip for `price` and returns the result as a floating point number.\n", "1. `round_up_to_nearest_dollar(price)`: Accepts 1 argument via the parameter `price`, a float representing the cost of the activity. Rounds `price` to the nearest dollar and returns the result as a floating point number.\n", "1. `park_car()`: Accepts no arguments. Prompts the user for the cost to park their car and returns the result as a floating point number. Calls the following functions:\n", " 1. `get_price()`\n", "1. `go_to_dinner(tax_percent)`: Accepts 1 argument via the parameter `tax_percent`, a float representing the sales tax percent. Computes the total cost of dinner (meal + tax + tip, rounded to the nearest dollar) and returns the result as a floating point number. Calls the following functions:\n", " 1. `get_price()`\n", " 1. `get_tip_percent()`\n", " 1. `calculate_tax()`\n", " 1. `calculate_tip()`\n", " 1. `round_up_to_nearest_dollar()`\n", "1. `go_to_concert(tax_percent)`: Accepts 1 argument via the parameter `tax_percent`, a float representing the sales tax percent. Computes the total cost of the concert (concert tickets + tax) and returns the result as a floating point number. Calls the following functions:\n", " 1. `get_price()`\n", " 1. `calculate_tax()`\n", "1. `display_money_spent(dinner_spent, concert_spent, parking_spent)`: Accepts 3 arguments via the parameters `dinner_spent`, `concert_spent`, and `parking_spent`, which represent how much money the user spent on dinner, the concert, and parking, respectively. Displays the results.\n", "1. `main()` function that drives the program. Calls the following functions:\n", " 1. `display_instructions()`\n", " 1. `get_tax_percent()`\n", " 1. `park_car()`\n", " 1. `go_to_dinner()`\n", " 1. `go_to_concert()`\n", " 1. `display_money_spent()`\n", "\n", "Feel free to define more functions as you see fit!" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### Example Run\n", "Here is an example run of the program:\n", "\n", "```\n", "Welcome to the night out simulator! This program will prompt you for the sales tax (as a percent) in your state. \n", "Then it will walk you through your night out on the town, starting with dinner and ending with a concert! \n", "The total amount of money spent for the evening will be kept track of, so you know how much your credit card bill will be!\n", "\n", "What is the sales tax in your state (as a percentage)? 7.8\n", "How much does parking cost? 1.25\n", "How much does dinner cost before tax? 49.99\n", "What percentage do you want to leave as a tip? 20\n", "How much does parking cost? 5.00\n", "How much does concert cost before tax? 75.50\n", "\n", "After tax and tip, you spent $65.00 on dinner.\n", "After tax, you spent $81.39 on the concert.\n", "In total, you spent $6.25 on parking.\n", "That means, you spent $152.64 this evening. Sounds like a lot of fun!\n", "```" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### Bonus (5 pts)\n", "[`matplotlib`](http://matplotlib.org/index.html) is a Python library that produces beautiful publication-quality visualizations. To use `matplotlib`, we simply need to import it: `import matplotlib`. I've written a function, `plot_money_spent()` that accepts 6 arguments, 3 strings representing the activity labels for the night out and 3 floating point numbers representing the total spent for each activity:" ] }, { "cell_type": "code", "execution_count": 1, "metadata": { "collapsed": true }, "outputs": [], "source": [ "import matplotlib.pyplot as plt\n", "\n", "def plot_money_spent(label1, price1, label2, price2, label3, price3):\n", " '''\n", " 111 STUDENTS: THIS IS THE FUNCTION YOU WILL CALL FOR THE **BONUS** TASK\n", " Accepts 3 pairs of strings (activity labels) and 3 floats (money spent)\n", " Ordering of the parameters is 3 pairs of activity label string, money spent value\n", " \n", " Uses matplotlib functions to plot a bar graph of the activity prices. \n", " Save the plot by clicking on the save button on the toolbar of the plot window.\n", " Press the X to close the window when you are done.\n", " \n", " This function does not return anything.\n", " '''\n", " x = [0, 1, 2] \n", " x_labels = [label1, label2, label3]\n", " y = [price1, price2, price3]\n", " plt.bar(x, y)\n", " plt.xticks(x, x_labels, ha='left')\n", " plt.xlabel(\"Activity\")\n", " plt.ylabel(\"Price in Dollars\")\n", " plt.tight_layout()\n", " # show the window\n", " plt.show()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Copy and paste the above code into your night_out.py file. **Do not modify the function `plot_money_spent()`.** Call `plot_money_spent()` passing in the activity label strings and money spent for each activity. The function will show a window that looks like the following:\n", "\n", "\n", "\n", "Save the plot as a .png image file by clicking on the save icon in the toolbar (highlighted in the screen shot above). Turn in your saved money spent plot image file in with your night_out.py file in your zip file." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Submitting Assignments\n", "1.\tUse the Blackboard tool https://learn.wsu.edu to submit your assignment to your TA. You will submit your code to the corresponding programming assignment under the \"Assignment Turn-In\" tab. You must upload your solutions as `_pa2.zip` by the due date and time.\n", "2.\tYour .zip file should contain your .py file and your .png file *if you attempted the bonus task*.\n", "\n", "**Note: By submitting your code to be graded, you are stating that your submission does not violate the CptS 111 Academic Integrity Policy outlined in the [Syllabus](http://nbviewer.jupyter.org/github/gsprint23/cpts111/blob/master/Syllabus.ipynb)**." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Grading Guidelines\n", "This assignment is worth 75 points + 5 points bonus. Your assignment will be evaluated based on a successful compilation and adherence to the program requirements. We will grade according to the following criteria:\n", "* 2 pts for correct `display_instructions()`\n", "* 4 pts for correct `get_tax_percent()`\n", "* 4 pts for correct `get_tip_percent()`\n", "* 5 pts for correct `get_price()`\n", "* 5 pts for correct `calculate_tax()`\n", "* 5 pts for correct `calculate_tip()`\n", "* 5 pts for correct `round_to_nearest_dollar()`\n", "* 5 pts for correct `park_car()`\n", "* 10 pts for correct `go_to_dinner()`\n", "* 10 pts for correct `go_to_concert()`\n", "* 5 pts for correct `display_money_spent()`\n", "* 10 pts for correct `main()` function that drives the program.\n", "* 5 pts for adherence to proper programming style and comments established for the class" ] } ], "metadata": { "anaconda-cloud": {}, "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.1" } }, "nbformat": 4, "nbformat_minor": 1 }