Python Program Help

profileeclipse831
HW9P_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 9" ] }, { "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": { "code_folding": [], "deletable": false, "editable": false, "nbgrader": { "cell_type": "code", "checksum": "1ae4de9780340bb521b73fe1f95e2689", "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": [ "**NOTE: SOME THINGS I WILL BE LOOKING FOR:**\n", "\n", "After each part of the assignment you will see a cell with a summary of the \"Instructor tests\" I will be doing.\n", "\n", "....And, of course I will be looking for efficient, well-written, readable code." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Problem 1: (4 questions)\n", "\n", "Write a program called ```main(filename)``` that reads from an input file called ```filename``` and creates a dictionary called ```capitals``` containing the U.S. states as keys, and their capitals as values. \n", "\n", "The program should then randomly quiz the user by displaying the name of a state and asking the user to enter that state's capital. \n", "\n", "The program should keep a count of the number of correct and incorrect responses. \n", "\n", "The ```main(filename)``` program should call the functions ```read_dictionary(filename)```, ```run_quiz(capitals)```, and ```show_quiz_results(capitals)``` to do its work.\n", "\n", "Here is the algorithm for the ```main(filename)``` function:\n", "```\n", "def main(filename):\n", "\n", " capitals = read_dictionary(filename)\n", "\n", " results_dct = run_quiz(capitals)\n", "\n", " show_quiz_results(results_dct)\n", "``` \n", "### Questions\n", "\n", "**Q1 - read_dictionary(filename)** function. \n", "```\n", "read_dictionary(filename): \n", "- Reads the contents of filename and creates and returns a dictionary of states and capitals\n", "```\n", "See further instructions below.\n", "\n", "**Q2 - run_quiz(capitals)** function. \n", "```\n", "run_quiz(capitals): \n", "- selects random states from the capitals dictionary, asks user for their answer, and reports the results as long as the user wants to continue. It returns a dictionary called results_dct with a summary of the quiz performance.\n", "```\n", "See further instructions below.\n", "\n", "**Q3 - show_quiz_results(results_dct)** function. \n", "```\n", "show_quiz_results(results_dct): \n", "- Displays the contents of the results_dct after a quiz run\n", "See the instructions below.\n", "```\n", "**Q4 - main(filename)** function. \n", "\n", "Finally, test that your code works by calling the ```read_dictionary(filename)```,```run_quiz(capitals)```, and ```show_quiz_results(result_dct)``` functions in the ```main(filename)``` function. \n", "\n", "Run the ```main(filename)``` function with the ```states_capital.txt``` file and check that your output matches the test provided. \n", "\n", "See further instructions below." ] }, { "cell_type": "markdown", "metadata": { "tags": [] }, "source": [ "### Points: 27 - read_dictionary(filename) function\n", "\n", "**Q1**\n", "\n", "Write and test a function called ```read_dictionary(filename)``` that takes ```filename``` and reads the contents of the file into a dictionary called ```capitals_dictionary```with values for the states and capitals. \n", "\n", "Use a try-except statement in your function to check for any ```Exception``` error (e.g. if the file is not found or could not be opened). Capture the error if it occurs in a variable called ```err``` and display the error message. \n", "\n", "The function should return ```capitals_dct```. If there is an error ```capitals_dct``` should be returned as an empty dictionary. \n", "\n", "Your function should give the following output when run with the following files (the files have been provided for you):\n", "```\n", "Filename: test_capitals.txt\n", "Number of lines = 2\n", "Result> Ok\n", "\n", "Filename: states_capitals.txt\n", "Number of lines = 50\n", "Result> Ok\n", "\n", "Filename: invalid.txt\n", "Got an Error> [Errno 2] No such file or directory: 'invalid.txt'\n", "States Captials = {}\n", "```\n", "the ```test_capitals.txt``` file only has two states. Check that the contents of the dictionary returned by the ```read_dictionary('test_capitals.txt`)``` function is is as follows:\n", "```\n", "{'Alabama': 'Montgomery', 'Alaska': 'Juneau'}\n", "```\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": "9e54fdaefbccff35f2957aac39e6c1d3", "grade": false, "grade_id": "Q1_answer", "locked": false, "schema_version": 3, "solution": true, "task": false }, "tags": [] }, "outputs": [], "source": [ "#Q1 [DO NOT DELETE!]\n", "\n", "# YOUR CODE HERE\n", "raise NotImplementedError()" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "code_folding": [] }, "outputs": [], "source": [ "# test for your answer - Try various cases for input file and check that your printout and dictionary match the scenarios\n", "# TEST CODE HERE\n", "capitals_dct = read_dictionary('test_capitals.txt')\n", "print(f'States Captials = {capitals_dct}')\n", "expected_dct = {'Alabama': 'Montgomery', 'Alaska': 'Juneau'}\n", "assert capitals_dct == expected_dct, \"Error: capitals_dct not correct\"\n", "print(\" Ok - dictionary output matches, check that printout matches\")\n", "print()\n", "\n", "capitals_dct = read_dictionary('states_capitals.txt')\n", "assert len(capitals_dct) == 50, \"Error: capitals_dct not correct\"\n", "print(\" Ok - dictionary output matches, check that printout matches\")\n", "print()\n", "\n", "capitals_dct = read_dictionary('invalid.txt')\n", "print(f'States Captials = {capitals_dct}')\n", "assert capitals_dct == {}, \"Error: capitals_dct not correct\"\n", "print(\" Ok - dictionary output matches, check that printout matches\")\n", "print()" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "code_folding": [] }, "outputs": [], "source": [ "# Instructor Tests\n", "# Check read_dictionary() returns capitals dictionary and correct print output for test_capitals.txt\n", "# Check read_dictionary() returns capitals dictionary and correct print output for states_capitals.txt\n", "# Check read_dictionary() returns capitals dictionary and correct print output for invalid.txt" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "code_folding": [], "deletable": false, "editable": false, "nbgrader": { "cell_type": "code", "checksum": "4f1f96a03ea1525cca39b4913fac0a8d", "grade": true, "grade_id": "Q1_test_1", "locked": true, "points": 9, "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": "a27cd7769f2e9b1f7d26c65816e46c67", "grade": true, "grade_id": "Q1_test_2", "locked": true, "points": 9, "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": "78bfdadaf7e972cc2f80d84530f4b2de", "grade": true, "grade_id": "Q1_test_3", "locked": true, "points": 9, "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": "69a7fd7b2ee240c2bcc71ef8032c1a32", "grade": true, "grade_id": "Q1_test_summary", "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": { "tags": [] }, "source": [ "### Points: 54 - run_quiz(capitals)\n", "\n", "**Q2**, Write and test the ```run_quiz(capitals)``` function. The tests are provided for you in cells below. See the following instructions.\n", "```\n", "run_quiz(capitals): \n", "- Returns a dictionary called results_dct with a summary of the results of the quiz. The keys for the results_dct are:\n", "\n", "'attempts' - number of questions attempted\n", "'points' - total of correct answers (one point per correct answer)\n", "'remaining_Q' - number of questions that remained in dictionary when the user quit\n", "'right' - a dictionary holding the 'state' and 'capital' for the correctly answered questions\n", "'wrong' - a dictionary holding the 'state' and 'capital' for the incorrectly answered questions\n", "```\n", "Here's an example of output for a dictionary with only two states, one question attempted, one correct answer, the state, capital for the correctly answered questions and the incorrectly answered questions:\n", "\n", "```\n", "{'attempts' : 1, \n", "'points' : 1, \n", "'remaining_Q': 1, \n", "'right' : {'Alabama': 'Montgomery'}, \n", "'wrong' : {}}\n", "```\n", "Here's a possible alogorithm:\n", "```\n", "\n", " # initialize attempts and points to 0, and create empty dictionaries to hold wrong answers, correct answers and \n", " # summary results\n", "\n", " # while not done\n", " \n", " # get a random quiz question state and capital from dictionary\n", " # use the random.choice() function to select a quiz question\n", " \n", " # display quiz question\n", "\n", " # get user answer and check that answer and correct answer match - answers should be case insensitive\n", " # increment points if correct else indicate answer is wrong and show correct answer\n", " # store the correct answers in the correct answers dictionary and incorrect answers in the wrong answers \n", " # dictionary\n", " \n", " # display attempts, points and percentage of correct attempts\n", "\n", " # don't repeat quiz question - delete the question asked and show number states left\n", " \n", " # ask user if they want to continue and set the done flag accordingly\n", " \n", " # stop if there are no more capitals in the dictionary \n", " \n", " # return the results (attempts, points, questions remaining, right answers, wrong answers) in a dictionary\n", " called results_dct \n", " \n", " return results_dct\n", "```\n", "\n", "**Note**: Use the ```random.choice()``` function to randomly select your quiz question. I will be looking for this in your code.\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": "db42498e0cb4902b5dc1bb1e3df60df7", "grade": false, "grade_id": "Q2_answer_1", "locked": false, "schema_version": 3, "solution": true, "task": false }, "tags": [] }, "outputs": [], "source": [ "#Q2 [DO NOT DELETE!]\n", "import random\n", "\n", "# YOUR CODE HERE\n", "raise NotImplementedError()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Test 1: For the following inputs:\n", "When the dictionary is:\n", "```\n", "test_dct = {'Alabama': 'Montgomery', 'Alaska': 'Juneau'}\n", "```\n", "and the inputs are:\n", "```\n", "Montgomery y Juneau y\n", "```\n", "Your output should look as follows:\n", "```\n", "Question: What is the capital of: Alabama\n", "Montgomery\n", "CORRECT!\n", "Attempts: 1, Points: 1, Percentage: 100.0%\n", "States left in quiz: 1\n", "Continue [y/n]: y\n", "Question: What is the capital of: Alaska\n", "Juneau\n", "CORRECT!\n", "Attempts: 2, Points: 2, Percentage: 100.0%\n", "States left in quiz: 0\n", "Continue [y/n]: y\n", "That's it you went through all the states!\n", "```" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Test 2: For the following inputs:\n", "When the dictionary is:\n", "```\n", "test_dct = {'Alabama': 'Montgomery', 'Alaska': 'Juneau'}\n", "```\n", "and the inputs are (notice the lowercase input) for state:\n", "```\n", "montgomery y Austin n\n", "```\n", "Your output should look as follows:\n", "```\n", "Question: What is the capital of: Alabama\n", "Montgomery\n", "CORRECT!\n", "Attempts: 1, Points: 1, Percentage: 100.0%\n", "States left in quiz: 1\n", "Continue [y/n]: y\n", "Question: What is the capital of: Alaska\n", "Austin\n", "Sorry. The capital is: Juneau\n", "Attempts: 2, Points: 1, Percentage: 50.0%\n", "States left in quiz: 0\n", "Continue [y/n]: n\n", "That's it you went through all the states!\n", "```" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "code_folding": [] }, "outputs": [], "source": [ "# test for your answer - Try the two test scenarios above\n", "# TEST CODE HERE\n", "import random\n", "random.seed(1) # set to make sure same selections are made every time you run function\n", "test_dct = {'Alabama': 'Montgomery', 'Alaska': 'Juneau'}\n", "results_dct = run_quiz(test_dct)\n", "print()\n", "print(f'Results of quiz:\\n{results_dct}')" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "code_folding": [] }, "outputs": [], "source": [ "# Instructor Tests\n", "# Check that run_quiz returns correct results for a small dictionary - 2 correct answers, y,y\n", "# Check that run_quiz returns correct results for a small dictionary - 1 correct,1 wrong answer, y,n\n", "# Check that run_quiz returns correct results for a small dictionary - 2 correct answers one in lowercase, y,y\n", "# Check that run_quiz returns correct results for full dictionary\n", "# Check that run_quiz returns input/output that matches test case\n", "# Check that random.choice() is used" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "code_folding": [ 0 ], "deletable": false, "editable": false, "nbgrader": { "cell_type": "code", "checksum": "31330195b09bacbd8e508deaef5d3197", "grade": true, "grade_id": "Q2_test_1", "locked": true, "points": 9, "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": "3e630dd9078ef194f5d3f7512d84ceea", "grade": true, "grade_id": "Q2_test_2", "locked": true, "points": 9, "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": "a0c394aa28c8f0abc9cebd18a313838f", "grade": true, "grade_id": "Q2_test_3", "locked": true, "points": 9, "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": "38b8ec5b7e0530d7a34d9cd7dd81bf1e", "grade": true, "grade_id": "Q2_test_4", "locked": true, "points": 9, "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": "f11e967c64a01dc9cb86ac6e645185ad", "grade": true, "grade_id": "Q2_test_5", "locked": true, "points": 9, "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": "60b861de2450fee7eab812513f2b4c7e", "grade": true, "grade_id": "Q2_test_6", "locked": true, "points": 9, "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": "3a63e25e71bc528807b907d0de2783c2", "grade": true, "grade_id": "Q2_test_sumary", "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": { "tags": [] }, "source": [ "### Points: 6 - show_quiz_results(results_dct)\n", "\n", "**Q3**. Write and test the ```show_quiz_results(results_dct)``` function as indicated in the instructions below.\n", "```\n", "show_quiz_results(results_dct): \n", "- Displays the contents of the results_dct\n", "```\n", "Test 1: Here's an example of output for a results dictionary where **no** questions were missed. if the ```results_dct``` is:\n", "```\n", "results_dct = {'attempts' : 1, \n", " 'points' : 1, \n", " 'remaining_Q': 1, \n", " 'right' : {'Alabama': 'Montgomery'}, \n", " 'wrong' : {}\n", " }\n", "```\n", "Your function should display the following output:\n", "```\n", "Attempted : 1\n", "Correct : 1\n", "Correct % : 100.0%\n", "Remaining Q : 1\n", "Excellent - Perfect Score!!!\n", "Bye - I will miss you!\n", "```\n", "Test 2: Here's an example of output for a results dictionary where questions were missed. if the ```results_dct``` is:\n", "```\n", "results_dct = {'attempts' : 2, \n", " 'points' : 1, \n", " 'remaining_Q': 0, \n", " 'right' : {'Alabama': 'Montgomery'}, \n", " 'wrong' : {'Alaska': 'Juneau'}\n", "```\n", "Your function should display the following output:\n", "```\n", "Attempted : 2\n", "Correct : 1\n", "Correct % : 50.0%\n", "Remaining Q : 0\n", "Here are the questions you missed:\n", " Alaska : Juneau\n", "Bye - I will miss you!\n", "\n", "```\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": "bee9400f7f2fcb908c0d950da710c790", "grade": false, "grade_id": "Q3_answer", "locked": false, "schema_version": 3, "solution": true, "task": false }, "tags": [] }, "outputs": [], "source": [ "#Q3 [DO NOT DELETE!]\n", "\n", "# YOUR CODE HERE\n", "raise NotImplementedError()" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "code_folding": [], "tags": [] }, "outputs": [], "source": [ "# test for your answer - run the function with a results dictionary\n", "# TEST CODE HERE\n", "# try with no missed questions\n", "results_dct = {'attempts' : 1, \n", " 'points' : 1, \n", " 'remaining_Q': 1, \n", " 'right' : {'Alabama': 'Montgomery'}, \n", " 'wrong' : {}\n", " }\n", "show_quiz_results(results_dct)\n", "print()\n", "\n", "# try with some missed questions\n", "results_dct = {'attempts' : 2, \n", " 'points' : 1, \n", " 'remaining_Q': 0, \n", " 'right' : {'Alabama': 'Montgomery'}, \n", " 'wrong' : {'Alaska': 'Juneau'}\n", " }\n", "show_quiz_results(results_dct)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "code_folding": [] }, "outputs": [], "source": [ "# Instructor Tests\n", "# Check that functions prints output that matches for no missed questions\n", "# Check that functions prints output that matches for missed questions" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "code_folding": [], "deletable": false, "editable": false, "nbgrader": { "cell_type": "code", "checksum": "aec4c2b72575ad02bb9ec9a4249a0f36", "grade": true, "grade_id": "Q3_test_1", "locked": true, "points": 3, "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": "4d945e1392bbb7f64cfa474f33cefb65", "grade": true, "grade_id": "Q3_test_2", "locked": true, "points": 3, "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: 3 - main(filename) function\n", " \n", "**Q4**. Write and test the ```main(filename)``` function as indicated in the instructions above.\n", "\n", "When testing you should set ```random.seed(1)``` and run your ```main(filename)``` program using ```filename``` = 'states_capitals.txt'\n", "\n", "You should match the input/output in the test below.\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": "1f666cc88ace47b1e50b0bec679809a0", "grade": false, "grade_id": "Q4_answer", "locked": false, "schema_version": 3, "solution": true, "task": false }, "tags": [] }, "outputs": [], "source": [ "#Q4 [DO NOT DELETE!]\n", "# YOUR CODE HERE\n", "raise NotImplementedError()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Test: For the following inputs:\n", "When the filename is 'states_capitals.txt' and the inputs are:\n", "```\n", "Tallahassee y harrisburg y San Diego n\n", "```\n", "Your output should look as follows:\n", "```\n", "Filename: states_capitals.txt\n", "Number of lines = 50\n", "Result> Ok\n", "Question: What is the capital of: Florida\n", "Tallahassee\n", "CORRECT!\n", "Attempts: 1, Points: 1, Percentage: 100.0%\n", "States left in quiz: 49\n", "Continue [y/n]: y\n", "Question: What is the capital of: Pennsylvania\n", "harrisburg\n", "CORRECT!\n", "Attempts: 2, Points: 2, Percentage: 100.0%\n", "States left in quiz: 48\n", "Continue [y/n]: y\n", "Question: What is the capital of: California\n", "San Diego\n", "Sorry. The capital is: Sacramento\n", "Attempts: 3, Points: 2, Percentage: 66.7%\n", "States left in quiz: 47\n", "Continue [y/n]: n\n", "Attempted : 3\n", "Correct : 2\n", "Correct % : 66.7%\n", "Remaining Q : 47\n", "Here are the questions you missed:\n", " California : Sacramento\n", "Bye - I will miss you!\n", "```" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "code_folding": [] }, "outputs": [], "source": [ "# test for your answer - run the main() program for valid and invalid files\n", "# TEST CODE HERE\n", "import random\n", "random.seed(1)\n", "filename = 'states_capitals.txt'\n", "main(filename)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "code_folding": [] }, "outputs": [], "source": [ "# Instructor Tests\n", "# Check that main(filename) return the expected output for a given input" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "code_folding": [], "deletable": false, "editable": false, "nbgrader": { "cell_type": "code", "checksum": "02d35109f889fcd612ebbc7f5aa3eb97", "grade": true, "grade_id": "Q4_test_1", "locked": true, "points": 3, "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": "ebbf355d2261e4f100ac5424d7ee9c34", "grade": false, "grade_id": "Q5_style_check", "locked": true, "points": 10, "schema_version": 3, "solution": false, "task": true }, "tags": [] }, "source": [ "### Points: 10 - Style\n", "**Q5**. Manually graded. Is code well written, readable?" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "code_folding": [], "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 (ipykernel)", "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.9.7" }, "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 }