{ "cells": [ { "cell_type": "markdown", "metadata": { "collapsed": true }, "source": [ "## Code tracing and debugging\n", "\n", "\n", "Once you come up with an idea for a code, it is almost never the case that your first implementation will work. When this happens, you need to take a breath and start debugging.\n", "\n", "Debugging is basically tracing your program in your head (often with the help of some pen and paper) for some particular input, and finding out where and why it fails. Obviously you should choose an input that makes your program fail, but something small enough so that you don't need to run too many steps until failing. Your input should be **as simple as possible and as complicated as necessary**.\n", "\n", "If you happen to have a computer around, you can use it to help you trace the code by adding `print` statements to your code." ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Hello world!\n" ] } ], "source": [ "print(\"Hello world!\")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "`print` is a python command used to print things to the console. \n", "\n", "Whatever is in the parentheses is going to be printed. If the thing in parentheses is in quotes, it is going to be printed *ipsis litteris* (word per work). If the thing in parentheses is a variable, python prints the value that is inside that variable." ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "10\n" ] } ], "source": [ "x = 10\n", "print(x)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Quotes and variables can be combined inside prints to make the messages more understandable. A comma indicates there is a space between the parts." ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "The answer to the life, universe, and everything is 42 .\n" ] } ], "source": [ "x = 42\n", "print(\"The answer to the life, universe, and everything is\", x, \".\")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Note that this does not alter the flow of your program. Python does nothing to whatever is printed, and this is only for your scrutinity." ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "while 1\n", "n = 42\n", "x = 0\n", " while 2\n", " n = 4\n", " x = 4\n", " while 2\n", " n = 0\n", " x = 20\n", "while 1\n", "n = 20\n", "x = 0\n", " while 2\n", " n = 2\n", " x = 0\n", " while 2\n", " n = 0\n", " x = 4\n" ] }, { "data": { "text/plain": [ "False" ] }, "execution_count": 4, "metadata": {}, "output_type": "execute_result" } ], "source": [ "def mystery(n):\n", " x = 0\n", " while (x != 1 and x != 4):\n", " x = 0\n", " print(\"while 1\")\n", " print(\"n =\", n)\n", " print(\"x =\", x)\n", " while (n != 0):\n", " x = x + (n % 10) ** 2\n", " n = n // 10\n", " print(\" while 2\")\n", " print(\" n =\", n)\n", " print(\" x =\", x)\n", " n = x\n", " if x == 1:\n", " return True\n", " else:\n", " return False\n", " \n", "mystery(42)" ] } ], "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.8.3" } }, "nbformat": 4, "nbformat_minor": 2 }