Python Program Help

profileeclipse831
HW4P_problem1.ipynb

{ "cells": [ { "cell_type": "markdown", "metadata": { "deletable": false, "editable": false, "nbgrader": { "cell_type": "markdown", "checksum": "7b47b6d141b4a60d79c2a115b8a69043", "grade": false, "grade_id": "cell-568582423fb764a7", "locked": true, "schema_version": 3, "solution": false, "task": false }, "tags": [] }, "source": [ "# INSTRUCTIONS:\n", "Before you turn this Lab in, make sure everything runs as expected. First, **restart the kernel** (in the menubar, select Kernel$\\rightarrow$Restart) and then **run all cells** (in the menubar, select Cell$\\rightarrow$Run All).\n", "\n", "**TIP**: Open another jupyter notebook to try out your code and experiment with your answers. You can then copy your answer into the Lab notebook for your final submission. This will reduce the chance that your submitted Lab will not be correctly graded.\n", "\n", "Make sure you fill in any place that says `YOUR CODE HERE` or \"YOUR ANSWER HERE\", and follow the instructions carefully.\n", "Also enter your name in the markdown cell below:" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "NAME = \"\"" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "---" ] }, { "cell_type": "markdown", "metadata": { "tags": [] }, "source": [ "# CS1411-Introduction to Programming with Python\n", "## Homework 4.1" ] }, { "cell_type": "markdown", "metadata": { "tags": [] }, "source": [ "**SUBMISSION GUIDELINES**\n", "\n", "1. First design, develop and test your code in a Jupyter notebook or other development environment\n", " - You can expirement and try different things in that notebook\n", "2. Then copy your final code and markdown cells into the Jupyter Notebook file (.ipynb) provided for the assignment and submit to Canvas\n", " - **Your submission file should be named the same as the download file**\n", " - I must be able to open and run your notebook in order to grade it\n", "3. Note that the Jupyter notebook provided for final submission may contain testing code to help you check that your output and expected match. \n", " - Follow the instructions in the notebook for copying your code and running the testing code\n", " - The instructor may run additional tests to check that your code runs correctly\n", "4. If asked, also provide any supporting files or images requested in the assignment\n", "\n", "**GRADING CRITERIA:**\n", "1. Good documentation/comments and program readability using both markdown cells and code comments\n", "2. Algorithm/pseudo-code is explained in a markdown cell and is efficiently written\n", "3. Program runs correctly for test cases with no syntax errors or logical errors\n", "\n", "***The instructor should be able to reproduce your work from your notebook.***" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "deletable": false, "editable": false, "nbgrader": { "cell_type": "code", "checksum": "3d46f7cb2f3540eeaa5255b5fb22c8b1", "grade": false, "grade_id": "cell-e05854b6a32ee6ea", "locked": true, "schema_version": 3, "solution": false, "task": false }, "tags": [] }, "outputs": [], "source": [ "# Run this to check that you have the correct version of Notebook\n", "import IPython\n", "assert IPython.version_info[0] >= 3, \"Your version of IPython is too old, please update it.\"" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "deletable": false, "editable": false, "nbgrader": { "cell_type": "code", "checksum": "a8aec40eed898a46b49b523d128a2d4a", "grade": false, "grade_id": "cell-570ebf38cad6e650", "locked": true, "schema_version": 3, "solution": false, "task": false }, "tags": [] }, "outputs": [], "source": [ "\"\"\" Run this setup cell - It is used to keep track of the points per question and set up test environment.\"\"\"\n", "\"\"\" Do not copy/duplicate this cell\"\"\"\n", "num_questions = 10\n", "points = { 1.0:[0,10.0], 2.01:[0, 8.0], 2.02:[0,8.0], 2.03:[0,8.0], 2.04:[0,8.0], \n", " 2.05:[0, 8.0], 2.06:[0, 8.0], 2.07:[0,8.0], 2.08:[0,8.0],\n", " 2.09:[0, 4.0], 2.10:[0, 4.0], 2.11:[0,4.0], 2.12:[0,4.0], 3.0:[0,10.0]\n", " }\n", "print(f\"***** TOTAL POINTS = {sum([value[1] for value in points.values()])}****\")\n", "\n", "# For instructor use in setting up test environment\n", "# class input_vars:\n", "# input_list = []\n", "# L = input_vars()" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "deletable": false, "editable": false, "nbgrader": { "cell_type": "code", "checksum": "562c66d73a448e8812b7848aaa22a4f8", "grade": true, "grade_id": "Instructor_test_setup", "locked": true, "points": 0, "schema_version": 3, "solution": false, "task": false }, "tags": [] }, "outputs": [], "source": [ "\"\"\" This cell has the instructors test setup and is hidden - DO NOT copy/duplicate this cell\"\"\"" ] }, { "cell_type": "markdown", "metadata": { "tags": [] }, "source": [ "### Problem 1: Retirement Calculator\n", "\n", "Simple Retirement Calculator: Growth of annual investment over a time horizon\n", "\n", "Write a program to ask the user to enter an initial investment, ```start_principal```, in \\$, an annual savings contribution at the start of each year, ```annual_savings```, in $, an annual investment rate of return ,```interest_rate_pct```, in %, and the number of years, ```years_to_invest```, to save for. \n", "\n", "Use float type for ```start_principal```, ```annual_savings```, and ```interest_rate_pct```, and int type for ```years_to_invest```. Also ask the user to input the ```print_interval``` as an integer which will be used to format the years to show in an output table.\n", "\n", "So I am expecting to find, and will be looking in your program for these variable names: ```start_principal```, ```annual_savings```, ```interest_rate_pct```, ```years_to_invest```, and ```print_interval```.\n", "\n", "If the user enters 10000, 5000, 7, 30, and 5, your prompts and inputs should look as follows:\n", "```\n", "Start Invest : 10000\n", "Contribution : 5000\n", "Return : 7\n", "Years : 30\n", "Print Interval : 5\n", "```\n", "\n", "The program should calculate the total principal invested and save the result in a variable called ```total_principal_invested```. The formula for doing this is:\n", "```\n", "total principal invested = start principal + (annual_savings x years to invest)\n", "```\n", "\n", "The program should then calculate and display the investment balance at the end of every ```print_interval``` years of the investment horizon and display the results in a nicely formatted table. See the output of the sample test run below.\n", "\n", "To calculate the starting investment balance for any given year, take the ending balance at the end of the previous year, add the annual savings amount, and multiply the total by (1+ interest rate in decimal). (*Note for simplicity we assume that the annual savings occurs at the start of every year*).\n", "\n", "For example, if the initial investment is \\\\$10,000, the annual savings is \\\\$5,000, and the annual return is 10% then the first 3 years of your calculations would look as follows:\n", "```\t\t\t\t\t\n", "Year Start Invest Contribution % Return $ Return End Investment\n", " 0 10,000.00 5,000.00 10.00 1,500.00 16,500.00\n", " 1 16,500.00 5,000.00 10.00 2,150.00 23,650.00\n", " 2 23,650.00 5,000.00 10.00 2,865.00 31,515.00\n", " 3 31,515.00 5,000.00 10.00 3,651.50 40,166.50\n", "```\n", "\n", "Use a ```for``` loop with ```range()``` to generate the annual end investment for each year of the scenario. Display the output as a formatted table and make the table more compact by printing year 0 and only every ```print_interval``` years of output, as well as the final year. Do not use tabs to create your table. Use f-strings to format your output.\n", "\n", "At the end of your for loop you should have an end investment number. Save it in a variable called ```end_investment``` that I can check.\n", "\n", "Calculate the the investment return and save in the variable ```investment_return```. The formula for the investment return is:\n", "```\n", "investment return = end investment - total principal invested\n", "```\n", "\n", "Test your function by matching the expected output ***exactly*** for the given inputs. Check your spelling and spacing carefully. You can try the following test inputs (the instructor may run additional and/or different scenarios to test your code):\n", "\n", "Try : start_principal = 10000, annual_savings = 5000, interest_rate_pct = 7, years_to_invest = 32, print_interval = 10\n", "\n", "Here is the expected output to match exactly:\n", "```\n", "***Your Scenario***\n", "Start Invest : 10,000.00\n", "Contribution : 5,000.00\n", "Return : 7.00%\n", "Years : 32\n", "Print Interval: 10\n", "\n", "Total of initial investment plus annual savings = $170,000.00\n", "\n", "Year Start Invest Contrib % Ret $ Ret End Invest\n", " 0 10,000.00 5,000.00 7.00 1,050.00 16,050.00\n", " 10 93,589.51 5,000.00 7.00 6,901.27 105,490.78\n", " 20 258,022.73 5,000.00 7.00 18,411.59 281,434.32\n", " 30 581,487.76 5,000.00 7.00 41,054.14 627,541.90\n", " 32 676,819.83 5,000.00 7.00 47,727.39 729,547.22\n", "\n", "The end investment is = $729,547.22\n", "Investment Return = $559,547.22\n", "```\n", "\n", "**NOTE**: Make sure you have variables ```total_principal_invested```,```end_investment```, and ```investment_return``` in your program with the correct calculated result for the input scenario. I will be testing different scenarios and checking the content of these variables as well as ```start_principal```,```annual_savings```,```interest_rate_pct```, ```years_to_invest```, and ```print_interval```." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Points: 10 - Algorithm" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Q1.0 [Don't Delete!] - Enter your Algorithm steps below\n", "### My Algorithm Steps\n", "\n", "1. ...\n", "2. ...\n", "3. ...\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "code_folding": [ 0 ], "deletable": false, "editable": false, "nbgrader": { "cell_type": "code", "checksum": "0f1da1ef39375e0d084905bcec651f36", "grade": true, "grade_id": "Q1_test", "locked": true, "points": 10, "schema_version": 3, "solution": false, "task": false }, "tags": [] }, "outputs": [], "source": [ "\"\"\" This cell has the instructors test and is hidden - DO NOT copy/duplicate this cell\"\"\"" ] }, { "cell_type": "markdown", "metadata": { "tags": [] }, "source": [ "### Points: 90 \n", "- 10 points for well written/readable code\n", "- 80 for passing tests\n", "\n", "Q2. Write a program in the indicated cell below that generates an investment scenario for a given set of inputs. (Follow instructions above)\n", "\n", "(**DO NOT split your solution across cells**. Your complete code should be in the indicated answer cell)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "code_folding": [], "deletable": false, "nbgrader": { "cell_type": "code", "checksum": "dccaeaffc59ea18238540897d6d3a559", "grade": false, "grade_id": "Q2_1_answer", "locked": false, "schema_version": 3, "solution": true, "task": false }, "tags": [] }, "outputs": [], "source": [ "#Q2.01 [DO NOT DELETE!]\n", "\n", "# YOUR CODE HERE\n", "raise NotImplementedError()" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "code_folding": [] }, "outputs": [], "source": [ "# Instructor Tests\n", "# Check inputs are correctly obtained\n", "# check using for & range() and not a while loop\n", "# check using f-strings to format\n", "# Check correct input types\n", "# Check output matches exactly for given inputs\n", "# Check the total_principal_invested is correct\n", "# Check the end_investment is correct\n", "# Check the investment_return is correct\n", "# Check investment return scenario 1 - 0, 5000, 5, 20\n", "# Check investment return scenario 2 - 0, 5000, 7, 30\n", "# Check investment return scenario 3 - 0, 5000, 7, 40\n", "# Check investment return scenario 4 - 0, 5000, 10, 40\n", "# Check style, readability, efficiency" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "code_folding": [ 0 ], "deletable": false, "editable": false, "nbgrader": { "cell_type": "code", "checksum": "c7b7227535c333fe6455b25404987446", "grade": true, "grade_id": "Q2_01_test", "locked": true, "points": 8, "schema_version": 3, "solution": false, "task": false }, "tags": [] }, "outputs": [], "source": [ "\"\"\" This cell has the instructors test and is hidden - DO NOT copy/duplicate this cell\"\"\"" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "code_folding": [ 0 ], "deletable": false, "editable": false, "nbgrader": { "cell_type": "code", "checksum": "2f0261e4803db91a37cd4fa481ea9af5", "grade": true, "grade_id": "Q2_02_test", "locked": true, "points": 8, "schema_version": 3, "solution": false, "task": false }, "tags": [] }, "outputs": [], "source": [ "\"\"\" This cell has the instructors test and is hidden - DO NOT copy/duplicate this cell\"\"\"" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "code_folding": [ 0 ], "deletable": false, "editable": false, "nbgrader": { "cell_type": "code", "checksum": "dd15adf815f985e1c241b31562572ec0", "grade": true, "grade_id": "Q2_03_test", "locked": true, "points": 8, "schema_version": 3, "solution": false, "task": false }, "tags": [] }, "outputs": [], "source": [ "\"\"\" This cell has the instructors test and is hidden - DO NOT copy/duplicate this cell\"\"\"" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "code_folding": [ 0 ], "deletable": false, "editable": false, "nbgrader": { "cell_type": "code", "checksum": "c58bd9b0134d165de96e6e115e15112a", "grade": true, "grade_id": "Q2_04_test", "locked": true, "points": 8, "schema_version": 3, "solution": false, "task": false }, "tags": [] }, "outputs": [], "source": [ "\"\"\" This cell has the instructors test and is hidden - DO NOT copy/duplicate this cell\"\"\"" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "code_folding": [ 0 ], "deletable": false, "editable": false, "nbgrader": { "cell_type": "code", "checksum": "a340b6c722fe25b0c9710dc2afbfda4d", "grade": true, "grade_id": "Q2_05_test", "locked": true, "points": 8, "schema_version": 3, "solution": false, "task": false }, "tags": [] }, "outputs": [], "source": [ "\"\"\" This cell has the instructors test and is hidden - DO NOT copy/duplicate this cell\"\"\"" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "code_folding": [ 0 ], "deletable": false, "editable": false, "nbgrader": { "cell_type": "code", "checksum": "5a430bfecbf0fcba9f1cb63ce3c33e9a", "grade": true, "grade_id": "Q2_06_test", "locked": true, "points": 8, "schema_version": 3, "solution": false, "task": false }, "tags": [] }, "outputs": [], "source": [ "\"\"\" This cell has the instructors test and is hidden - DO NOT copy/duplicate this cell\"\"\"" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "code_folding": [ 0 ], "deletable": false, "editable": false, "nbgrader": { "cell_type": "code", "checksum": "f1b91c05344ee953c2762f35ba69f808", "grade": true, "grade_id": "Q2_07_test", "locked": true, "points": 8, "schema_version": 3, "solution": false, "task": false }, "tags": [] }, "outputs": [], "source": [ "\"\"\" This cell has the instructors test and is hidden - DO NOT copy/duplicate this cell\"\"\"" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "code_folding": [ 0 ], "deletable": false, "editable": false, "nbgrader": { "cell_type": "code", "checksum": "8f436a38756449304d0f166231de638e", "grade": true, "grade_id": "Q2_08_test", "locked": true, "points": 8, "schema_version": 3, "solution": false, "task": false }, "tags": [] }, "outputs": [], "source": [ "\"\"\" This cell has the instructors test and is hidden - DO NOT copy/duplicate this cell\"\"\"" ] }, { "cell_type": "markdown", "metadata": { "tags": [] }, "source": [ "### Points: 16 - Running scenarios\n", "Q4. Now use the code you have written to run the following investment scenarios and assign the result, to two decimal places (as proper python numbers), of the ```investment return``` to the corresponding variables in the answer cell below. Here are the four scenarios to try:\n", "\n", "**scenario1: start_principal = 0, annual_savings = 5000, interest_rate_pct = 5, years_to_invest = 20**\n", "- save your result in the variable ```scenario1_0_5000_5_20```\n", "\n", "**scenario2: start_principal = 0, annual_savings = 5000, interest_rate_pct = 7, years_to_invest = 30**\n", "- save your result in the variable ```scenario2_0_5000_7_30```\n", "\n", "**scenario3: start_principal = 0, annual_savings = 5000, interest_rate_pct = 7, years_to_invest = 40**\n", "- save your result in the variable ```scenario3_0_5000_7_40```\n", "\n", "**scenario4: start_principal = 0, annual_savings = 5000, interest_rate_pct = 10, years_to_invest = 40**\n", "- save your result in the variable ```scenario4_0_5000_10_40```" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "#Q2.09 [DO NOT DELETE!]\n", "\n", "scenario1_0_5000_5_20 = None\n", "scenario2_0_5000_7_30 = None\n", "scenario3_0_5000_7_40 = None\n", "scenario4_0_5000_10_40 = None" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "code_folding": [ 0 ], "deletable": false, "editable": false, "nbgrader": { "cell_type": "code", "checksum": "2e1cbe0f42a04631d71505d40d381e2e", "grade": true, "grade_id": "Q2_09_test", "locked": true, "points": 4, "schema_version": 3, "solution": false, "task": false }, "tags": [] }, "outputs": [], "source": [ "\"\"\" This cell has the instructors test and is hidden - DO NOT copy/duplicate this cell\"\"\"" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "code_folding": [], "deletable": false, "editable": false, "nbgrader": { "cell_type": "code", "checksum": "a17cdeaf414a987f57df816ff38eb977", "grade": true, "grade_id": "Q2_10_test", "locked": true, "points": 4, "schema_version": 3, "solution": false, "task": false }, "tags": [] }, "outputs": [], "source": [ "\"\"\" This cell has the instructors test and is hidden - DO NOT copy/duplicate this cell\"\"\"" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "code_folding": [], "deletable": false, "editable": false, "nbgrader": { "cell_type": "code", "checksum": "14ffd74cb98c91270f73d9005ebc13fd", "grade": true, "grade_id": "Q2_11_test", "locked": true, "points": 4, "schema_version": 3, "solution": false, "task": false }, "tags": [] }, "outputs": [], "source": [ "\"\"\" This cell has the instructors test and is hidden - DO NOT copy/duplicate this cell\"\"\"" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "code_folding": [], "deletable": false, "editable": false, "nbgrader": { "cell_type": "code", "checksum": "29a4d4480e284d3a564b757b745ad039", "grade": true, "grade_id": "Q2_12_test", "locked": true, "points": 4, "schema_version": 3, "solution": false, "task": false }, "tags": [] }, "outputs": [], "source": [ "\"\"\" This cell has the instructors test and is hidden - DO NOT copy/duplicate this cell\"\"\"" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "deletable": false, "editable": false, "nbgrader": { "cell_type": "code", "checksum": "4ff638321f931bcbf624aaeb55f63bf2", "grade": true, "grade_id": "Q2_solution_feedback", "locked": true, "points": 0, "schema_version": 3, "solution": false, "task": false }, "tags": [] }, "outputs": [], "source": [ "\"\"\" This cell has the instructors test and is hidden - DO NOT copy/duplicate this cell\"\"\"" ] }, { "cell_type": "markdown", "metadata": { "deletable": false, "editable": false, "nbgrader": { "cell_type": "markdown", "checksum": "afe510e1eda6227998518f35db138095", "grade": false, "grade_id": "Q3_style_check", "locked": true, "points": 10, "schema_version": 3, "solution": false, "task": true }, "tags": [] }, "source": [ "### Points: 10 - Style\n", "Q3. Manually graded. Is code well written, readable?" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "code_folding": [ 0 ], "deletable": false, "editable": false, "nbgrader": { "cell_type": "code", "checksum": "e7f372ee0ffb63bbdc7fc03c94be8ade", "grade": true, "grade_id": "Q3_style_test", "locked": true, "points": 0, "schema_version": 3, "solution": false, "task": false }, "tags": [] }, "outputs": [], "source": [ "\"\"\" This cell has the instructors test and is hidden - DO NOT copy/duplicate this cell\"\"\"" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "deletable": false, "editable": false, "nbgrader": { "cell_type": "code", "checksum": "861b7f37d8e18e9c2c972896561a93b1", "grade": true, "grade_id": "Summary_points", "locked": true, "points": 0, "schema_version": 3, "solution": false, "task": false }, "tags": [] }, "outputs": [], "source": [ "\"\"\" This cell has the instructors test and is hidden - DO NOT copy/duplicate this cell\"\"\"" ] } ], "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.3" }, "toc": { "base_numbering": 1, "nav_menu": {}, "number_sections": true, "sideBar": true, "skip_h1_title": false, "title_cell": "Table of Contents", "title_sidebar": "Contents", "toc_cell": false, "toc_position": {}, "toc_section_display": true, "toc_window_display": false }, "varInspector": { "cols": { "lenName": 16, "lenType": 16, "lenVar": 40 }, "kernels_config": { "python": { "delete_cmd_postfix": "", "delete_cmd_prefix": "del ", "library": "var_list.py", "varRefreshCmd": "print(var_dic_list())" }, "r": { "delete_cmd_postfix": ") ", "delete_cmd_prefix": "rm(", "library": "var_list.r", "varRefreshCmd": "cat(var_dic_list()) " } }, "oldHeight": 122.85, "position": { "height": "174.85px", "left": "888.08px", "right": "20px", "top": "116px", "width": "225.72px" }, "types_to_exclude": [ "module", "function", "builtin_function_or_method", "instance", "_Feature" ], "varInspector_section_display": "block", "window_display": false } }, "nbformat": 4, "nbformat_minor": 4 }