{ "cells": [ { "cell_type": "markdown", "metadata": { "collapsed": false, "nbgrader": { "grade": false, "grade_id": "6e76b3", "locked": true, "schema_version": 3, "solution": false, "task": false } }, "source": [ "# Assignment 2\n", "## This is an ungraded assignment!\n", "## Deadline: 1st Oct - 15:00 - After this time, don't change your notebook. It'll be automatically collected by CoCalc.\n", "\n", "### We will use this assignment to simulate how a graded assignment works. On the deadline, we'll automatically collect your assignment and calculate the points. As an answer, you will receive a report showing the test cases where you succeeded and the final number of points. However, remember, as this is a non-graded assignment, those points won't count for your grade.\n" ] }, { "cell_type": "markdown", "metadata": { "collapsed": false, "nbgrader": { "grade": false, "grade_id": "6fd6a2", "locked": true, "schema_version": 3, "solution": false, "task": false } }, "source": [ "
\n", " What to do if you get stuck with a task?\n", "
\n", "" ] }, { "cell_type": "markdown", "metadata": { "collapsed": false, "nbgrader": { "grade": false, "grade_id": "task1", "locked": true, "schema_version": 3, "solution": false, "task": false } }, "source": [ "
Task 0 (0 Points)
\n", "Write code that prints the string 'Hello world'." ] }, { "cell_type": "code", "execution_count": 1, "metadata": { "collapsed": false, "nbgrader": { "grade": true, "grade_id": "task1", "locked": false, "points": 0, "schema_version": 3, "solution": true, "task": false } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Hello world\n" ] } ], "source": [ "### BEGIN SOLUTION\n", "print(\"Hello world\")\n", "### END SOLUTION" ] }, { "cell_type": "markdown", "metadata": { "collapsed": false, "nbgrader": { "grade": false, "grade_id": "08c914", "locked": true, "schema_version": 3, "solution": false, "task": false } }, "source": [ "
\n", " How to read error messages and debug on CoCalc\n", "
\n", "There will be times when you see an error message because of something being wrong in your code.
\n", " This is completely normal!\n", "
\n", "A lot of the time when programming is spent fixing errors in the code or looking for more efficient solutions. So there will definitely be times during this course when you run into errors. But how can you understand and fix them?\n" ] }, { "cell_type": "code", "execution_count": 1, "metadata": { "collapsed": false, "nbgrader": { "grade": false, "grade_id": "9c1846", "locked": true, "schema_version": 3, "solution": false, "task": false } }, "outputs": [ { "ename": "NameError", "evalue": "name 'num' is not defined", "output_type": "error", "traceback": [ "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", "\u001b[0;31mNameError\u001b[0m Traceback (most recent call last)", "\u001b[0;32m\u001b[0m in \u001b[0;36m\u001b[0;34m\u001b[0m\n\u001b[1;32m 1\u001b[0m \u001b[0mnumber\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0;36m3\u001b[0m \u001b[0;34m*\u001b[0m \u001b[0;36m5\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m----> 2\u001b[0;31m \u001b[0mprint\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mnum\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m", "\u001b[0;31mNameError\u001b[0m: name 'num' is not defined" ] } ], "source": [ "number = 3 * 5\n", "print(num)" ] }, { "cell_type": "markdown", "metadata": { "collapsed": false, "nbgrader": { "grade": false, "grade_id": "295172", "locked": true, "schema_version": 3, "solution": false, "task": false } }, "source": [ "The cell above shows such an example. The stack trace / traceback is shown. The last line tells you that a NameError was encountered when your code was executed. We can check the Python documentation to see what this error means - link to the documentation: [NameError](https://docs.python.org/3/library/exceptions.html#NameError) - or we can read the message that is included with it: name 'number' is not defined.\n", "\n", "Above the error we can see the code that caused the error. In this case the error happened in line 2. Based on the code in that line and the error message we can see that the variable $number$ was not defined. When we tried to print it the error happened. To fix it can we can rename the variable $num$ in line 1 to $number$ or change the $number$ in line 2 to $num$." ] }, { "cell_type": "code", "execution_count": 2, "metadata": { "collapsed": false, "nbgrader": { "grade": false, "grade_id": "5f3222", "locked": true, "schema_version": 3, "solution": false, "task": false } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "15\n" ] } ], "source": [ "num = 3 * 5\n", "print(num)" ] }, { "cell_type": "markdown", "metadata": { "collapsed": false, "nbgrader": { "grade": false, "grade_id": "task1", "locked": true, "schema_version": 3, "solution": false, "task": false } }, "source": [ "
Task 1 (1 Point)
\n", "Define a variable s1." ] }, { "cell_type": "code", "execution_count": 8, "metadata": { "collapsed": false, "nbgrader": { "grade": false, "grade_id": "task1_answer", "locked": false, "schema_version": 3, "solution": true, "task": false } }, "outputs": [ ], "source": [ "### BEGIN SOLUTION\n", "s1 = None\n", "### END SOLUTION" ] }, { "cell_type": "code", "execution_count": 9, "metadata": { "collapsed": false, "nbgrader": { "grade": true, "grade_id": "task1_test_variableDefined", "locked": true, "points": 1, "schema_version": 3, "solution": false, "task": false } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "\u001b[6;30;42mSuccess!\u001b[0m\n" ] } ], "source": [ "# If you see no errors under this cell then your solution is correct!\n", "# You don't have to do anything with the code in this cell. It's just here to check your answer.\n", "\n", "# Tests for Task 1\n", "from nose.tools import assert_true\n", "assert_true(\"s1\" in locals(), msg=\"You did not define a variable called 's1'.\")\n", "print(\"\\x1b[6;30;42mSuccess!\\x1b[0m\")" ] }, { "cell_type": "markdown", "metadata": { "collapsed": false, "nbgrader": { "grade": false, "grade_id": "a77e4b", "locked": true, "schema_version": 3, "solution": false, "task": false } }, "source": [ "# The following cell contains an example of what you see when your solution is wrong.\n", "\n", "Big red scary box = something is wrong!\n", "\n", "This is just here as an example to show you what it looks like. You don't have to change the code in the box to make it work or anything." ] }, { "cell_type": "code", "execution_count": 3, "metadata": { "collapsed": false, "nbgrader": { "grade": true, "grade_id": "test_fail_example", "locked": true, "points": 0, "schema_version": 3, "solution": false, "task": false } }, "outputs": [ { "ename": "AssertionError", "evalue": "False is not true", "output_type": "error", "traceback": [ "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", "\u001b[0;31mAssertionError\u001b[0m Traceback (most recent call last)", "\u001b[0;32m\u001b[0m in \u001b[0;36m\u001b[0;34m\u001b[0m\n\u001b[1;32m 1\u001b[0m \u001b[0;32mfrom\u001b[0m \u001b[0mnose\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mtools\u001b[0m \u001b[0;32mimport\u001b[0m \u001b[0massert_true\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m----> 2\u001b[0;31m \u001b[0massert_true\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;32mFalse\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m", "\u001b[0;32m/usr/lib/python3.8/unittest/case.py\u001b[0m in \u001b[0;36massertTrue\u001b[0;34m(self, expr, msg)\u001b[0m\n\u001b[1;32m 763\u001b[0m \u001b[0;32mif\u001b[0m \u001b[0;32mnot\u001b[0m \u001b[0mexpr\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 764\u001b[0m \u001b[0mmsg\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0m_formatMessage\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mmsg\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;34m\"%s is not true\"\u001b[0m \u001b[0;34m%\u001b[0m \u001b[0msafe_repr\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mexpr\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m--> 765\u001b[0;31m \u001b[0;32mraise\u001b[0m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mfailureException\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mmsg\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 766\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 767\u001b[0m \u001b[0;32mdef\u001b[0m \u001b[0m_formatMessage\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mself\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mmsg\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mstandardMsg\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n", "\u001b[0;31mAssertionError\u001b[0m: False is not true" ] } ], "source": [ "from nose.tools import assert_true\n", "assert_true(False)" ] }, { "cell_type": "markdown", "metadata": { "collapsed": false, "nbgrader": { "grade": false, "grade_id": "b75ec0", "locked": true, "schema_version": 3, "solution": false, "task": false } }, "source": [ "Here we see that the error is an AssertionError. The lower half of the stack trace shows us code that is called by the code that we have written. This useful when we are using code from other projects in our own projects. In this case we don't need it though.\n", "\n", "The structure of the stack trace is that at the bottom it shows the line of code that caused the error, and then above that code block there can be more code blocks, with code that has caused the code in the lower block to be called.\n", "\n", "In our example (from top to bottom): 'assert_true(False)' was executed in line 2, which caused the code in the lower half to be executed, where in line 765 an error happened.\n", "\n", "The error further tells us that 'False is not true'. In the upper half we see that 'assert_true(False)' was executed in line 2. This means that the value inside 'assert_true()' should have been 'True' and not 'False'.\n", "\n", "The tests that check your code will usually check that the value that your code calculated is the same as the correct answer. That way you can find out which value is wrong and by looking at your code then figure out why. Another example is given below." ] }, { "cell_type": "code", "execution_count": 10, "metadata": { "collapsed": false, "nbgrader": { "grade": false, "grade_id": "064e11", "locked": true, "schema_version": 3, "solution": false, "task": false } }, "outputs": [ { "ename": "AssertionError", "evalue": "0 != 2", "output_type": "error", "traceback": [ "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", "\u001b[0;31mAssertionError\u001b[0m Traceback (most recent call last)", "\u001b[0;32m\u001b[0m in \u001b[0;36m\u001b[0;34m\u001b[0m\n\u001b[1;32m 7\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 8\u001b[0m \u001b[0;32mfrom\u001b[0m \u001b[0mnose\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mtools\u001b[0m \u001b[0;32mimport\u001b[0m \u001b[0massert_equal\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m----> 9\u001b[0;31m \u001b[0massert_equal\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mc\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;36m2\u001b[0m\u001b[0;34m)\u001b[0m \u001b[0;31m# check that 'c' has the value 2\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m", "\u001b[0;32m/usr/lib/python3.8/unittest/case.py\u001b[0m in \u001b[0;36massertEqual\u001b[0;34m(self, first, second, msg)\u001b[0m\n\u001b[1;32m 910\u001b[0m \"\"\"\n\u001b[1;32m 911\u001b[0m \u001b[0massertion_func\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0m_getAssertEqualityFunc\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mfirst\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0msecond\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m--> 912\u001b[0;31m \u001b[0massertion_func\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mfirst\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0msecond\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mmsg\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0mmsg\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 913\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 914\u001b[0m \u001b[0;32mdef\u001b[0m \u001b[0massertNotEqual\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mself\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mfirst\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0msecond\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mmsg\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0;32mNone\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n", "\u001b[0;32m/usr/lib/python3.8/unittest/case.py\u001b[0m in \u001b[0;36m_baseAssertEqual\u001b[0;34m(self, first, second, msg)\u001b[0m\n\u001b[1;32m 903\u001b[0m \u001b[0mstandardMsg\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0;34m'%s != %s'\u001b[0m \u001b[0;34m%\u001b[0m \u001b[0m_common_shorten_repr\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mfirst\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0msecond\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 904\u001b[0m \u001b[0mmsg\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0m_formatMessage\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mmsg\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mstandardMsg\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m--> 905\u001b[0;31m \u001b[0;32mraise\u001b[0m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mfailureException\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mmsg\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 906\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 907\u001b[0m \u001b[0;32mdef\u001b[0m \u001b[0massertEqual\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mself\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mfirst\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0msecond\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mmsg\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0;32mNone\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n", "\u001b[0;31mAssertionError\u001b[0m: 0 != 2" ] } ], "source": [ "# Task: Divide 'a' by 'b' and store the value in the variable 'c'.\n", "a = 10\n", "b = 5\n", "c = 0\n", "\n", "a / b # Divide a by b (but we forget to store the value in 'c')\n", "\n", "from nose.tools import assert_equal\n", "assert_equal(c, 2) # check that 'c' has the value 2" ] }, { "cell_type": "markdown", "metadata": { "collapsed": false, "nbgrader": { "grade": false, "grade_id": "2f03ef", "locked": true, "schema_version": 3, "solution": false, "task": false } }, "source": [ "We get another 'AssertionError'. '0 != 2'. Again the lower half of the stack trace doesn't really help us. In the first block (from the top) we see our code, where we are checking that 'c' is equal to '2'. Because we get an 'AssertionError' we know that this is not the case, so 'c' is not equal to '2'. The error further tells us that apparently 'c' must be 0. \n", "\n", "We can then look at our calculation code again and we see that we forgot to save the value of 'a / b' in the variable c! If we save the the value correctly in 'c' and run the cells again then the error will disappear, as our solution is now correct.\n", "\n", "Play around with the code in the next cell and see how the error changes if you change the value for 'c'." ] }, { "cell_type": "code", "execution_count": 4, "metadata": { "collapsed": false }, "outputs": [ ], "source": [ "# This cell is here for you to experiment with how the test cases work\n", "# This cell (the box that this text and the code are in) is not part of the assignment, it is just here so you can experiment easier.\n", "# You can add your own cells if you want to experiment by moving your mouse to the upper \"edge\" of the cell (it should turn blue) and clicking\n", "# This is useful when you want to try something!\n", "\n", "# Task: Divide 'a' by 'b' and store the value in the variable 'c'.\n", "a = 10\n", "b = 5\n", "c = 0\n", "\n", "c = a / b # Divide a by b (we fixed our code)\n", "\n", "from nose.tools import assert_equal\n", "assert_equal(c, 2) # check that 'c' has the value 2" ] }, { "cell_type": "markdown", "metadata": { "collapsed": false, "nbgrader": { "grade": false, "grade_id": "task2", "locked": true, "schema_version": 3, "solution": false, "task": false } }, "source": [ "
Task 2 (1 Point)
\n", "Change the type of the variable 'rating' to int." ] }, { "cell_type": "code", "execution_count": 5, "metadata": { "cocalc": { "outputs": { "0": { "name": "input", "opts": { "password": false, "prompt": "Enter an integer rating between 1 and 10" }, "output_type": "stream", "value": "5" } } }, "collapsed": false, "nbgrader": { "grade": false, "grade_id": "task2_answer", "locked": false, "schema_version": 3, "solution": true, "task": false } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": "Enter an integer rating between 1 and 10 5" } ], "source": [ "rating = \"5\"\n", "t = type(rating)\n", "print(\"The variable 'rating' has the type: \", t) # You can also do: print(\"The variable 'rating' has the type: \", type(rating))\n", "### BEGIN SOLUTION\n", "rating = int(rating)\n", "x = type(rating)\n", "### END SOLUTION\n", "# The type should now be 'int' instead of 'str'\n", "print(\"The variable 'rating' has the type: \", x)" ] }, { "cell_type": "code", "execution_count": 16, "metadata": { "collapsed": false, "nbgrader": { "grade": true, "grade_id": "task2_tests", "locked": true, "points": 1, "schema_version": 3, "solution": false, "task": false } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "\u001b[6;30;42mSuccess!\u001b[0m\n" ] } ], "source": [ "# If you see no errors under this cell your solution is correct!\n", "\n", "# Tests for Task2\n", "from nose.tools import assert_equal, assert_true\n", "\n", "# Two ways to check the type of a variable: with type() or with isinstance()\n", "assert_equal(type(rating), int)\n", "assert_true(isinstance(rating, int))\n", "\n", "print(\"\\x1b[6;30;42mSuccess!\\x1b[0m\")" ] }, { "cell_type": "markdown", "metadata": { "collapsed": false, "nbgrader": { "grade": false, "grade_id": "task3", "locked": true, "schema_version": 3, "solution": false, "task": false } }, "source": [ "
Task 3
\n", "For a circle of radius 3, display the diameter, circumference and area. Use the value 3.14159 for $\\pi$. Use the following formulas (r is the radius):\n", "
    \n", "
  • $diameter = 2 r$
  • \n", "
  • $circumference = 2 \\pi r$
  • \n", "
  • $area = \\pi r^{2}$
  • \n", "
\n", "Create variables called $diameter$, $circumference$ and $area$ that contain the calculated values for the circle of radius 3. Calculate the values in your code, don't calculate the values by hand and create variables that are assigned the values." ] }, { "cell_type": "code", "execution_count": 11, "metadata": { "collapsed": false, "nbgrader": { "grade": false, "grade_id": "task3_answer", "locked": false, "schema_version": 3, "solution": true, "task": false } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "6 18.849539999999998 28.27431\n" ] } ], "source": [ "### BEGIN SOLUTION\n", "r = 3\n", "pi = 3.14159\n", "diameter = 2 * r\n", "circumference = 2 * pi * r\n", "area = pi * (r ** 2)\n", "print(diameter, circumference, area)\n", "### END SOLUTION" ] }, { "cell_type": "code", "execution_count": 12, "metadata": { "collapsed": false, "nbgrader": { "grade": true, "grade_id": "task3_tests_variablesDefined", "locked": true, "points": 0.5, "schema_version": 3, "solution": false, "task": false } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "\u001b[6;30;42mSuccess!\u001b[0m\n" ] } ], "source": [ "# Tests for Task 3\n", "from nose.tools import assert_true\n", "assert_true(\"diameter\" in locals(), msg=\"You did not define a variable called 'diameter'!\")\n", "assert_true(\"circumference\" in locals(), msg=\"You did not define a variable called 'circumference'!\")\n", "assert_true(\"area\" in locals(), msg=\"You did not define a variable called 'area'!\")\n", "\n", "print(\"\\x1b[6;30;42mSuccess!\\x1b[0m\")" ] }, { "cell_type": "code", "execution_count": 13, "metadata": { "collapsed": false, "nbgrader": { "grade": true, "grade_id": "task3_tests_calculationResults", "locked": true, "points": 0.5, "schema_version": 3, "solution": false, "task": false } }, "outputs": [ ], "source": [ "# Tests for Task 3\n", "from nose.tools import assert_equal\n", "assert_equal(diameter, 6, msg=\"Diameter was calculated incorrectly!\")\n", "assert_equal(circumference, 18.849539999999998, msg=\"Circumference was calculated incorrectly!\")\n", "assert_equal(area, 28.27431, msg=\"Area was calculated incorrectly!\")" ] }, { "cell_type": "markdown", "metadata": { "collapsed": false, "nbgrader": { "grade": false, "grade_id": "task4", "locked": true, "schema_version": 3, "solution": false, "task": false } }, "source": [ "
\n", " Task 4\n", "
\n", "Use $if$ statements that change the value of the variable $check$ to $True$ if the variable $num$ is even and changes the value of the variable $check$ to $False$ if the variable $num$ is odd.\n", "You should use $True$ and $False$ for this task. Set the value of $check$ to $True$ or $False$ in your if-condition.\n", "You can use $else$ and $elif$ if you need them, not just $if$.\n", "\n", "Hint: Use the remainder operator (%, also known as [modulo](https://en.wikipedia.org/wiki/Modulo_operation))." ] }, { "cell_type": "code", "execution_count": 14, "metadata": { "collapsed": false, "nbgrader": { "grade": false, "grade_id": "boolean_example", "locked": true, "schema_version": 3, "solution": false, "task": false } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Is True the same as the string 'True'? False\n", "Is False the same as the string 'False'? False\n" ] } ], "source": [ "# An example of True and False:\n", "t = True\n", "f = False\n", "\n", "# They are not strings!\n", "print(\"Is True the same as the string 'True'?\", True == \"True\")\n", "print(\"Is False the same as the string 'False'?\", False == \"False\")" ] }, { "cell_type": "code", "execution_count": 1, "metadata": { "collapsed": false, "nbgrader": { "grade": false, "grade_id": "task4_answer", "locked": false, "schema_version": 3, "solution": true, "task": false } }, "outputs": [ ], "source": [ "def is_even(num):\n", " check = None\n", " # Put your if-conditions down there!\n", " ### BEGIN SOLUTION\n", " if num % 2 == 0: #num is even\n", " check = True\n", " else: #num is odd\n", " check = False \n", " ### END SOLUTION\n", " # Put your if-conditions up there!\n", " return check" ] }, { "cell_type": "code", "execution_count": 2, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "True" ] }, "execution_count": 2, "metadata": { }, "output_type": "execute_result" } ], "source": [ "is_even(2)" ] }, { "cell_type": "code", "execution_count": 16, "metadata": { "collapsed": false, "nbgrader": { "grade": true, "grade_id": "task4_test_functionDefinedNotOverwritten", "locked": true, "points": 0.5, "schema_version": 3, "solution": false, "task": false } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "\u001b[6;30;42mSuccess!\u001b[0m\n" ] } ], "source": [ "from nose.tools import assert_true, assert_false\n", "import inspect\n", "assert_true(inspect.isfunction(is_even), msg=\"The 'is_even' function does not exist anymore. Did you overwrite it by using 'is_even' as a variable name?\")\n", "print(\"\\x1b[6;30;42mSuccess!\\x1b[0m\")" ] }, { "cell_type": "code", "execution_count": 17, "metadata": { "collapsed": false, "nbgrader": { "grade": true, "grade_id": "task4_tests_checkFunctionResults", "locked": true, "points": 0.5, "schema_version": 3, "solution": false, "task": false } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "\u001b[6;30;42mSuccess!\u001b[0m\n" ] } ], "source": [ "# If you see no errors under this cell then your solution is correct!\n", "\n", "# Tests for task4\n", "from nose.tools import assert_true, assert_false\n", "assert_false(is_even(2) is None, msg=\"You do not have a 'return' statement in your function. Did you delete it? The last line of your function should be 'return check'.\")\n", "\n", "assert_true(is_even(2))\n", "assert_true(is_even(42))\n", "assert_true(is_even(23218232))\n", "assert_false(is_even(1))\n", "assert_false(is_even(41))\n", "assert_false(is_even(21832831))\n", "print(\"\\x1b[6;30;42mSuccess!\\x1b[0m\")" ] }, { "cell_type": "markdown", "metadata": { "collapsed": false, "nbgrader": { "grade": false, "grade_id": "04196a", "locked": false, "remove": true, "schema_version": 3, "solution": false, "task": false } }, "source": [ "This cell should only be visible to tutors (hopefully).\n", "\n", "The goal of Task 5 and 6 is to teach the students debugging / reading the stack traces." ] }, { "cell_type": "markdown", "metadata": { "collapsed": false, "nbgrader": { "grade": false, "grade_id": "task5", "locked": true, "schema_version": 3, "solution": false, "task": false } }, "source": [ "
\n", " Task 5 (4 Points, 1 Point per question)\n", "
\n", "\n", "What caused the error in the following code samples? Store your solution in a variable called $solution$. There is always only one correct solution.\n", "\n", "This task uses hidden>hidden tests. You won't know if your solution is correct or not until it is graded and returned." ] }, { "cell_type": "code", "execution_count": 18, "metadata": { "collapsed": false, "nbgrader": { "grade": false, "grade_id": "686a3f", "locked": true, "schema_version": 3, "solution": false, "task": false } }, "outputs": [ { "ename": "ZeroDivisionError", "evalue": "division by zero", "output_type": "error", "traceback": [ "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", "\u001b[0;31mZeroDivisionError\u001b[0m Traceback (most recent call last)", "\u001b[0;32m\u001b[0m in \u001b[0;36m\u001b[0;34m\u001b[0m\n\u001b[1;32m 1\u001b[0m \u001b[0ma\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0;36m5\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 2\u001b[0m \u001b[0mb\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0ma\u001b[0m \u001b[0;34m-\u001b[0m \u001b[0;36m5\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m----> 3\u001b[0;31m \u001b[0ma\u001b[0m \u001b[0;34m/\u001b[0m \u001b[0mb\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m", "\u001b[0;31mZeroDivisionError\u001b[0m: division by zero" ] } ], "source": [ "a = 5\n", "b = a - 5\n", "a / b" ] }, { "cell_type": "code", "execution_count": 19, "metadata": { "collapsed": false, "nbgrader": { "grade": false, "grade_id": "task5_a", "locked": false, "schema_version": 3, "solution": true, "task": false } }, "outputs": [ ], "source": [ "# a) We forgot to define the variable 'c'.\n", "# b) We divide by zero.\n", "# c) We forgot to convert the type to 'int'.\n", "# d) We tried to do a calculation with an 'int' and with a 'str'.\n", "solution = \"a)\" # enter your solution here in this format\n", "### BEGIN SOLUTION\n", "solution = \"b)\"\n", "### END SOLUTION" ] }, { "cell_type": "code", "execution_count": 20, "metadata": { "collapsed": false, "nbgrader": { "grade": true, "grade_id": "task5_a_tests", "locked": true, "points": 1, "schema_version": 3, "solution": false, "task": false } }, "outputs": [ ], "source": [ "# Tests for Task 5a)\n", "# There is a hidden test that checks if your solution is correct\n", "\n", "# Checks that your chosen answer is one of the four possible options\n", "from nose.tools import assert_equal, assert_true\n", "assert_true(solution in [\"a)\", \"b)\", \"c)\", \"d)\"], msg=\"Your solution must be 'a)', 'b)', 'c)' or 'd)'.\")\n", "\n", "\n", "### BEGIN HIDDEN TESTS\n", "# students will NOT see these extra tests\n", "assert_equal(solution, \"b)\")\n", "### END HIDDEN TESTS" ] }, { "cell_type": "code", "execution_count": 7, "metadata": { "collapsed": false, "nbgrader": { "grade": false, "grade_id": "b7adae", "locked": true, "schema_version": 3, "solution": false, "task": false } }, "outputs": [ { "ename": "TypeError", "evalue": "unsupported operand type(s) for -: 'int' and 'str'", "output_type": "error", "traceback": [ "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", "\u001b[0;31mTypeError\u001b[0m Traceback (most recent call last)", "\u001b[0;32m\u001b[0m in \u001b[0;36m\u001b[0;34m\u001b[0m\n\u001b[1;32m 1\u001b[0m \u001b[0ma\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0;36m5\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m----> 2\u001b[0;31m \u001b[0mb\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0ma\u001b[0m \u001b[0;34m-\u001b[0m \u001b[0;34m\"5\"\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 3\u001b[0m \u001b[0mc\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0ma\u001b[0m \u001b[0;34m/\u001b[0m \u001b[0mb\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n", "\u001b[0;31mTypeError\u001b[0m: unsupported operand type(s) for -: 'int' and 'str'" ] } ], "source": [ "a = 5\n", "b = a - \"5\"\n", "c = a / b" ] }, { "cell_type": "code", "execution_count": 22, "metadata": { "collapsed": false, "nbgrader": { "grade": false, "grade_id": "task5_b_answer", "locked": false, "schema_version": 3, "solution": true, "task": false } }, "outputs": [ ], "source": [ "# a) We forgot to define the variable 'c'.\n", "# b) We divide by zero.\n", "# c) We forgot to convert the type to 'int'.\n", "# d) We tried to do a calculation with an 'int' and with a 'str'.\n", "solution = \"a)\" # enter your solution here in this format\n", "### BEGIN SOLUTION\n", "solution = \"d)\"\n", "### END SOLUTION" ] }, { "cell_type": "code", "execution_count": 23, "metadata": { "collapsed": false, "nbgrader": { "grade": true, "grade_id": "task5_b_tests", "locked": true, "points": 1, "schema_version": 3, "solution": false, "task": false } }, "outputs": [ ], "source": [ "# Tests for Task 5b)\n", "# There is a hidden test that checks if your solution is correct\n", "\n", "# Checks that your chosen answer is one of the four possible options\n", "from nose.tools import assert_equal, assert_true\n", "assert_true(solution in [\"a)\", \"b)\", \"c)\", \"d)\"], msg=\"Your solution must be 'a)', 'b)', 'c)' or 'd)'.\")\n", "\n", "\n", "### BEGIN HIDDEN TESTS\n", "# students will NOT see these extra tests\n", "assert_equal(solution, \"d)\")\n", "### END HIDDEN TESTS" ] }, { "cell_type": "code", "execution_count": 21, "metadata": { "collapsed": false, "nbgrader": { "grade": false, "grade_id": "269a90", "locked": true, "schema_version": 3, "solution": false, "task": false } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "The value of c is 3\n" ] } ], "source": [ "a = 5\n", "b = a - 3\n", "c = int(a - b) #now it's correct, change str to int\n", "print(\"The value of c is\", c) # check that c is actually a number\n", "\n", "from nose.tools import assert_equal\n", "assert_equal(type(c), int)" ] }, { "cell_type": "code", "execution_count": 22, "metadata": { "collapsed": false, "nbgrader": { "grade": false, "grade_id": "task5_c", "locked": false, "schema_version": 3, "solution": true, "task": false } }, "outputs": [ ], "source": [ "# a) We forgot to define the variable 'c'.\n", "# b) We divide by zero.\n", "# c) We forgot to convert the type to 'int'.\n", "# d) We tried to do a calculation with an 'int' and with a 'str'.\n", "solution = \"a)\" # enter your solution here in this format\n", "### BEGIN SOLUTION\n", "solution = \"c)\"\n", "### END SOLUTION" ] }, { "cell_type": "code", "execution_count": 26, "metadata": { "collapsed": false, "nbgrader": { "grade": true, "grade_id": "task5_c_tests", "locked": true, "points": 1, "schema_version": 3, "solution": false, "task": false } }, "outputs": [ ], "source": [ "# Tests for Task 5c)\n", "# There is a hidden test that checks if your solution is correct\n", "\n", "# Checks that your chosen answer is one of the four possible options\n", "from nose.tools import assert_equal, assert_true\n", "assert_true(solution in [\"a)\", \"b)\", \"c)\", \"d)\"], msg=\"Your solution must be 'a)', 'b)', 'c)' or 'd)'.\")\n", "\n", "\n", "### BEGIN HIDDEN TESTS\n", "# students will NOT see these extra tests\n", "assert_equal(solution, \"c)\")\n", "### END HIDDEN TESTS" ] }, { "cell_type": "code", "execution_count": 27, "metadata": { "collapsed": false, "nbgrader": { "grade": false, "grade_id": "task5_d", "locked": true, "remove": false, "schema_version": 3, "solution": false, "task": false } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "The value is 3\n" ] }, { "ename": "AssertionError", "evalue": "'3' != 3", "output_type": "error", "traceback": [ "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", "\u001b[0;31mAssertionError\u001b[0m Traceback (most recent call last)", "\u001b[0;32m\u001b[0m in \u001b[0;36m\u001b[0;34m\u001b[0m\n\u001b[1;32m 5\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 6\u001b[0m \u001b[0;32mfrom\u001b[0m \u001b[0mnose\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mtools\u001b[0m \u001b[0;32mimport\u001b[0m \u001b[0massert_equal\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m----> 7\u001b[0;31m \u001b[0massert_equal\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mc\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;36m3\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m", "\u001b[0;32m/usr/lib/python3.8/unittest/case.py\u001b[0m in \u001b[0;36massertEqual\u001b[0;34m(self, first, second, msg)\u001b[0m\n\u001b[1;32m 910\u001b[0m \"\"\"\n\u001b[1;32m 911\u001b[0m \u001b[0massertion_func\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0m_getAssertEqualityFunc\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mfirst\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0msecond\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m--> 912\u001b[0;31m \u001b[0massertion_func\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mfirst\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0msecond\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mmsg\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0mmsg\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 913\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 914\u001b[0m \u001b[0;32mdef\u001b[0m \u001b[0massertNotEqual\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mself\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mfirst\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0msecond\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mmsg\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0;32mNone\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n", "\u001b[0;32m/usr/lib/python3.8/unittest/case.py\u001b[0m in \u001b[0;36m_baseAssertEqual\u001b[0;34m(self, first, second, msg)\u001b[0m\n\u001b[1;32m 903\u001b[0m \u001b[0mstandardMsg\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0;34m'%s != %s'\u001b[0m \u001b[0;34m%\u001b[0m \u001b[0m_common_shorten_repr\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mfirst\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0msecond\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 904\u001b[0m \u001b[0mmsg\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0m_formatMessage\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mmsg\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mstandardMsg\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m--> 905\u001b[0;31m \u001b[0;32mraise\u001b[0m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mfailureException\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mmsg\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 906\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 907\u001b[0m \u001b[0;32mdef\u001b[0m \u001b[0massertEqual\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mself\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mfirst\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0msecond\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mmsg\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0;32mNone\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n", "\u001b[0;31mAssertionError\u001b[0m: '3' != 3" ] } ], "source": [ "a = 5\n", "b = a - 3 # 2\n", "cc = a - b # 3\n", "print(\"The value is \", cc) # checking that the value is really 3\n", "\n", "from nose.tools import assert_equal\n", "assert_equal(c, 3)" ] }, { "cell_type": "code", "execution_count": 28, "metadata": { "collapsed": false, "nbgrader": { "grade": false, "grade_id": "task5_d_answer", "locked": false, "schema_version": 3, "solution": true, "task": false } }, "outputs": [ ], "source": [ "# a) We forgot to define the variable 'c'.\n", "# b) We divide by zero.\n", "# c) We forgot to convert the type to 'int'.\n", "# d) We tried to do a calculation with an 'int' and with a 'str'.\n", "solution = \"a)\" # enter your solution here in this format\n", "### BEGIN SOLUTION\n", "solution = \"a)\"\n", "### END SOLUTION" ] }, { "cell_type": "code", "execution_count": 29, "metadata": { "collapsed": false, "nbgrader": { "grade": true, "grade_id": "task5_d_tests", "locked": true, "points": 1, "schema_version": 3, "solution": false, "task": false } }, "outputs": [ ], "source": [ "# Tests for Task 5d)\n", "# There is a hidden test that checks if your solution is correct\n", "\n", "# Checks that your chosen answer is one of the four possible options\n", "from nose.tools import assert_equal, assert_true\n", "assert_true(solution in [\"a)\", \"b)\", \"c)\", \"d)\"], msg=\"Your solution must be 'a)', 'b)', 'c)' or 'd)'.\")\n", "\n", "\n", "### BEGIN HIDDEN TESTS\n", "# students will NOT see these extra tests\n", "assert_equal(solution, \"a)\")\n", "### END HIDDEN TESTS" ] }, { "cell_type": "markdown", "metadata": { "collapsed": false, "nbgrader": { "grade": false, "grade_id": "task6", "locked": true, "schema_version": 3, "solution": false, "task": false } }, "source": [ "
\n", " Task 6\n", "
\n", "\n", "What caused the error in the following code samples? Store your solution in a variable called $solution$. There is always only one correct solution.\n", "\n", "This task uses hidden tests. You won't know if your solution is correct or not until it is graded and returned." ] }, { "cell_type": "code", "execution_count": 24, "metadata": { "collapsed": false, "nbgrader": { "grade": false, "grade_id": "task6_a", "locked": true, "schema_version": 3, "solution": false, "task": false } }, "outputs": [ { "ename": "AssertionError", "evalue": "False is not true", "output_type": "error", "traceback": [ "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", "\u001b[0;31mAssertionError\u001b[0m Traceback (most recent call last)", "\u001b[0;32m\u001b[0m in \u001b[0;36m\u001b[0;34m\u001b[0m\n\u001b[1;32m 13\u001b[0m \u001b[0massert_true\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0ma\u001b[0m \u001b[0;34m<\u001b[0m \u001b[0;36m6\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 14\u001b[0m \u001b[0massert_true\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mless_than_10\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m---> 15\u001b[0;31m \u001b[0massert_true\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mless_than_6\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m", "\u001b[0;32m/usr/lib/python3.8/unittest/case.py\u001b[0m in \u001b[0;36massertTrue\u001b[0;34m(self, expr, msg)\u001b[0m\n\u001b[1;32m 763\u001b[0m \u001b[0;32mif\u001b[0m \u001b[0;32mnot\u001b[0m \u001b[0mexpr\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 764\u001b[0m \u001b[0mmsg\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0m_formatMessage\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mmsg\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;34m\"%s is not true\"\u001b[0m \u001b[0;34m%\u001b[0m \u001b[0msafe_repr\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mexpr\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m--> 765\u001b[0;31m \u001b[0;32mraise\u001b[0m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mfailureException\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mmsg\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 766\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 767\u001b[0m \u001b[0;32mdef\u001b[0m \u001b[0m_formatMessage\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mself\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mmsg\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mstandardMsg\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n", "\u001b[0;31mAssertionError\u001b[0m: False is not true" ] } ], "source": [ "# Task: Write code that checks if variable 'a' is less than 10 and less than 6 and stores that information in two variables.\n", "a = 5\n", "less_than_10 = False\n", "less_than_6 = False\n", "if a < 10:\n", " less_than_10 = True\n", "elif a < 6:\n", " less_than_6 = True\n", "\n", "\n", "from nose.tools import assert_true\n", "assert_true(a < 10)\n", "assert_true(a < 6)\n", "assert_true(less_than_10)\n", "assert_true(less_than_6)" ] }, { "cell_type": "code", "execution_count": 25, "metadata": { "collapsed": false }, "outputs": [ ], "source": [ "#Bei If... elif... hört der Test beim ersten True auf und somit schon bei <10 --> deshalb elif zu if ändern" ] }, { "cell_type": "code", "execution_count": 31, "metadata": { "collapsed": false, "nbgrader": { "grade": false, "grade_id": "task6_a_answer", "locked": false, "schema_version": 3, "solution": true, "task": false } }, "outputs": [ ], "source": [ "# a) We used incorrect variable names. Numbers in variable names cause undefined, random behaviour and that is why the 'assert_true' failed.\n", "# b) Only one variable can have the value 'True'. We used up the 'True' for 'less_than_10', so we can't put it in 'less_than_6'.\n", "# c) We should have used an 'else' instead of 'elif'.\n", "# d) We should have used another 'if' instead of 'elif'.\n", "solution = \"a)\" # enter your solution here in this format\n", "### BEGIN SOLUTION\n", "solution = \"d)\"\n", "### END SOLUTION" ] }, { "cell_type": "code", "execution_count": 32, "metadata": { "collapsed": false, "nbgrader": { "grade": true, "grade_id": "task6_a_tests", "locked": true, "points": 1, "schema_version": 3, "solution": false, "task": false } }, "outputs": [ ], "source": [ "# Tests for Task 6a)\n", "# There is a hidden test that checks if your solution is correct\n", "\n", "# Checks that your chosen answer is one of the four possible options\n", "from nose.tools import assert_equal, assert_true\n", "assert_true(solution in [\"a)\", \"b)\", \"c)\", \"d)\"], msg=\"Your solution must be 'a)', 'b)', 'c)' or 'd)'.\")\n", "\n", "\n", "### BEGIN HIDDEN TESTS\n", "# students will NOT see these extra tests\n", "assert_equal(solution, \"d)\")\n", "### END HIDDEN TESTS" ] }, { "cell_type": "code", "execution_count": 27, "metadata": { "collapsed": false, "nbgrader": { "grade": false, "grade_id": "task6_b", "locked": true, "schema_version": 3, "solution": false, "task": false } }, "outputs": [ { "ename": "SyntaxError", "evalue": "invalid syntax (, line 4)", "output_type": "error", "traceback": [ "\u001b[0;36m File \u001b[0;32m\"\"\u001b[0;36m, line \u001b[0;32m4\u001b[0m\n\u001b[0;31m if a = 5:\u001b[0m\n\u001b[0m ^\u001b[0m\n\u001b[0;31mSyntaxError\u001b[0m\u001b[0;31m:\u001b[0m invalid syntax\n" ] } ], "source": [ "# Task: Write code that checks if the variable 'a' is equal to '5' (integer 5, not string 5) and if it is, sets the value of the variable 'equal_5' to 'True'.\n", "a = int(\"5\")\n", "equal_5 = False\n", "if a = 5:\n", " equal_5 = True\n", "\n", "\n", "from nose.tools import assert_true\n", "assert_true(equal_5)" ] }, { "cell_type": "code", "execution_count": 34, "metadata": { "collapsed": false, "nbgrader": { "grade": false, "grade_id": "task6_b_answer", "locked": false, "schema_version": 3, "solution": true, "task": false } }, "outputs": [ ], "source": [ "# a) We used a single equals sign. Single equals signs are used for value assignment instead of equality checking.\n", "# b) 'a' is a 'str' (string), not an 'int'. We can't check if an 'int' and a 'str' are the same.\n", "# c) We forgot to use brackets in the if-condition.\n", "# d) We forgot to add an 'else' or 'elif'. An 'if' without 'else' or 'elif' is invalid.\n", "solution = \"a)\" # enter your solution here in this format\n", "### BEGIN SOLUTION\n", "solution = \"a)\"\n", "### END SOLUTION" ] }, { "cell_type": "code", "execution_count": 35, "metadata": { "collapsed": false, "nbgrader": { "grade": true, "grade_id": "task6_b_tests", "locked": true, "points": 1, "schema_version": 3, "solution": false, "task": false } }, "outputs": [ ], "source": [ "# Tests for Task 6b)\n", "# There is a hidden test that checks if your solution is correct\n", "\n", "# Checks that your chosen answer is one of the four possible options\n", "from nose.tools import assert_equal, assert_true\n", "assert_true(solution in [\"a)\", \"b)\", \"c)\", \"d)\"], msg=\"Your solution must be 'a)', 'b)', 'c)' or 'd)'.\")\n", "\n", "\n", "### BEGIN HIDDEN TESTS\n", "# students will NOT see these extra tests\n", "assert_equal(solution, \"a)\")\n", "### END HIDDEN TESTS" ] }, { "cell_type": "markdown", "metadata": { "collapsed": false, "nbgrader": { "grade": false, "grade_id": "task7", "locked": true, "schema_version": 3, "solution": false, "task": false } }, "source": [ "
\n", " Task 7 (1 Point)\n", "
\n", "Store every letter of the string 'Programming' in the variable $s7$. Add a space character after every letter.\n", "\n", "In the end, the variable $s7$ should contain the value 'P r o g r a m m i n g '.\n", "\n", "Don't forget the space character the final 'g'!\n", "\n", "Hint: Use a for-loop for this task.\n", "\n", "Hint: You can combine strings, or add characters to them, with \"+\". An example is given in the cell below." ] }, { "cell_type": "code", "execution_count": 36, "metadata": { "collapsed": false, "nbgrader": { "grade": false, "grade_id": "b6734f", "locked": true, "schema_version": 3, "solution": false, "task": false } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Hello World\n", "----------------------------\n", "Hello World!\n" ] } ], "source": [ "tmp1 = \"Hello\"\n", "tmp2 = \"World\"\n", "tmp3 = tmp1 + \" \" + tmp2\n", "print(tmp3)\n", "print(\"----------------------------\")\n", "print(tmp3 + \"!\") # adding the '!' at the end" ] }, { "cell_type": "code", "execution_count": 33, "metadata": { "collapsed": false, "nbgrader": { "grade": false, "grade_id": "task7_answer", "locked": false, "schema_version": 3, "solution": true, "task": false } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "P r o g r a m m i n g \n" ] } ], "source": [ "p = \"Programming\"\n", "s7 = \"\" # this is called an empty string - a string with no characters in it\n", "### BEGIN SOLUTION\n", "for c in p:\n", " s7 += c + \" \"\n", "\n", "print(s7)\n", "### END SOLUTION" ] }, { "cell_type": "code", "execution_count": 38, "metadata": { "collapsed": false, "nbgrader": { "grade": true, "grade_id": "task7_tests", "locked": true, "points": 1, "schema_version": 3, "solution": false, "task": false } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "\u001b[6;30;42mSuccess!\u001b[0m\n" ] } ], "source": [ "# Tests for Task 7\n", "\n", "from nose.tools import assert_equal, assert_not_equal\n", "# this test checks that you didn't forget the space character at the end\n", "assert_not_equal(s7, \"P r o g r a m m i n g\", msg=\"You forgot the space character at the end.\")\n", "\n", "# this test checks that you didn't add a space character at the beginning\n", "assert_not_equal(s7, \" P r o g r a m m i n g \", msg=\"You have a space character before the 'P' that you don't need.\")\n", "\n", "# this test checks that your solution is correct\n", "# the other two tests are just there to help you find out where you made a mistake\n", "assert_equal(s7, \"P r o g r a m m i n g \")\n", "\n", "print(\"\\x1b[6;30;42mSuccess!\\x1b[0m\")" ] }, { "cell_type": "markdown", "metadata": { "collapsed": false, "nbgrader": { "grade": false, "grade_id": "task8", "locked": true, "schema_version": 3, "solution": false, "task": false } }, "source": [ "
\n", " Task 8 (1 Point)\n", "
\n", "Store every second letter of the string 'Programming' (starting at 'P') in the variable $s7$. Add a space character after every letter.\n", "\n", "In the end, the variable $s7$ should contain the value 'P o r m i g '.\n", "\n", "HINT: Use a for-loop for this task.\n", "\n", "HINT: Count how many iterations of the for-loop have been executed and use that information to check whether you should include the character of the current iteration in the result or not.\n", "\n", "Don't forget the space character the final 'g'!" ] }, { "cell_type": "code", "execution_count": 35, "metadata": { "collapsed": false, "nbgrader": { "grade": false, "grade_id": "task8_answer", "locked": false, "schema_version": 3, "solution": true, "task": false } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "P o r m i g \n" ] } ], "source": [ "p = \"Programming\"\n", "s7 = \"\" # this is called an empty string - a string with no characters in it\n", "### BEGIN SOLUTION\n", "count = 0\n", "for c in p:\n", " if count % 2 == 0:\n", " s7 += c + \" \"\n", " count += 1\n", "\n", "print(s7)\n", "### END SOLUTION" ] }, { "cell_type": "code", "execution_count": 40, "metadata": { "collapsed": false, "nbgrader": { "grade": true, "grade_id": "task8_tests", "locked": true, "points": 1, "schema_version": 3, "solution": false, "task": false } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "\u001b[6;30;42mSuccess!\u001b[0m\n" ] } ], "source": [ "# Tests for Task 8\n", "\n", "from nose.tools import assert_equal, assert_not_equal\n", "# this test checks that you didn't forget the space character at the end\n", "assert_not_equal(s7, \"P o r m i g\", msg=\"You forgot the space character at the end.\")\n", "\n", "# this test checks that you didn't add a space character at the beginning\n", "assert_not_equal(s7, \" P o r m i g \", msg=\"You have a space character before the 'P' that you don't need.\")\n", "\n", "# this test checks that your solution is correct\n", "# the other two tests are just there to help you find out where you made a mistake\n", "assert_equal(s7, \"P o r m i g \")\n", "\n", "print(\"\\x1b[6;30;42mSuccess!\\x1b[0m\")" ] }, { "cell_type": "markdown", "metadata": { "collapsed": false, "nbgrader": { "grade": false, "grade_id": "task9", "locked": true, "schema_version": 3, "solution": false, "task": false } }, "source": [ "
\n", " Task 9 (3 Points)\n", "
\n", "Write code that counts how many operations you need to perform to reach 1 with the Collatz conjecture sequence, for $n=11$. The example below has 6 steps.\n", "Store the number of steps required in a variable called $steps$.\n", "\n", "According to the Collatz conjecture the following sequence will always reach 1 for any positive integer:\n", "\n", "- if the integer is even then the next term in the sequence is half of the previous term\n", "- if the integer is odd then the next term in the sequence is 3 times the previous term plus 1\n", "\n", "\n", "Example for n=10:\n", "\n", "| Step | Current term | Next term |\n", "|---|---|---|\n", "|1| 10 | 5 (even, halve it) |\n", "|2| 5 | 16 (uneven, multiply by 3 and add 1): $next = (5 * 3) + 1 = 16$ |\n", "|3| 16 | 8 (even, halve it) |\n", "|4| 8 | 4 (even, halve it) |\n", "|5| 4 | 2 (even, halve it) |\n", "|6| 2 | 1 (we are finished) |\n", "\n", "This sequence can be described like this, perform this operation until you reach 1:\n", "\n", "$f(n) = \\begin{cases} \\frac{n}{2} &\\text{if } n \\equiv 0 \\pmod{2}\\\\[4px] 3n+1 & \\text{if } n\\equiv 1 \\pmod{2} .\\end{cases}$\n", "\n", "Hint: Use a while-loop.\n", "\n", "Hint: Use if-conditions inside the while-loop.\n", "\n", "Hint: Use the remainder operator (%, also known as [modulo](https://en.wikipedia.org/wiki/Modulo_operation)).\n", "\n", "Hint: Update the value of the variable $n$ in every loop-iteration (halve it if it is even, multiply by 3 and add 1 if it is odd).\n", "\n", "Hint: You do not have to define new variables for this task. You can solve it with only $n$ and $steps$.\n", "\n", "Hint: If you fail the test that checks your number of steps try replacing the example from above with $n=10$. Add print() statements to check which steps your code is executing and how many steps it needs! The test cases don't care about whether or not you have print()s in your solution, they won't break a correct solution but it can be helpful to figure out what your code does." ] }, { "cell_type": "code", "execution_count": 37, "metadata": { "collapsed": false, "nbgrader": { "grade": false, "grade_id": "task9_answer", "locked": false, "schema_version": 3, "solution": true, "task": false } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "1.0\n", "14\n" ] } ], "source": [ "n = 11\n", "steps = 0\n", "### BEGIN SOLUTION\n", "while n != 1:\n", " steps += 1\n", " if n % 2 == 0:\n", " n = n / 2\n", " else:\n", " n = (3 * n) + 1\n", "\n", "print(n)\n", "print(steps)\n", "### END SOLUTION" ] }, { "cell_type": "code", "execution_count": 42, "metadata": { "collapsed": false, "nbgrader": { "grade": true, "grade_id": "task9_tests", "locked": true, "points": 1, "schema_version": 3, "solution": false, "task": false } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "\u001b[6;30;42mSuccess!\u001b[0m\n" ] } ], "source": [ "# Tests for task 9\n", "\n", "from nose.tools import assert_equal\n", "assert_equal(n, 1, msg=\"n is not 1. Don't declare a new variable, update n in every loop iteration until it becomes 1.\")\n", "assert_equal(steps, 14, msg=\"You should need exactly 14 steps to get the result. Check the example in the table above to see what counts as a step.\")\n", "\n", "print(\"\\x1b[6;30;42mSuccess!\\x1b[0m\")" ] } ], "metadata": { "kernelspec": { "display_name": "Python 3 (system-wide)", "language": "python", "metadata": { "cocalc": { "description": "Python 3 programming language", "priority": 100, "url": "https://www.python.org/" } }, "name": "python3", "resource_dir": "/ext/jupyter/kernels/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.8.5" } }, "nbformat": 4, "nbformat_minor": 4 }