{ "metadata": { "name": "" }, "nbformat": 3, "nbformat_minor": 0, "worksheets": [ { "cells": [ { "cell_type": "heading", "level": 1, "metadata": {}, "source": [ "Errors" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Errors tell us when the program we've written isn't working properly, but they also tell us a lot about what is wrong with the program to help us fix it. Let's look at a simple example where we try to add a string and a number together. Python isn't sure what to do, so it tells us that we aren't allowed to do this by reporting an error." ] }, { "cell_type": "code", "collapsed": false, "input": [ "print(\"The temperature today is: \" + 75)" ], "language": "python", "metadata": {}, "outputs": [ { "ename": "TypeError", "evalue": "cannot concatenate 'str' and 'int' objects", "output_type": "pyerr", "traceback": [ "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m\n\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[0;32m----> 1\u001b[0;31m \u001b[0;32mprint\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m\"The temperature today is: \"\u001b[0m \u001b[0;34m+\u001b[0m \u001b[0;36m75\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m", "\u001b[0;31mTypeError\u001b[0m: cannot concatenate 'str' and 'int' objects" ] } ], "prompt_number": 1 }, { "cell_type": "markdown", "metadata": {}, "source": [ "The first line of the error tells us what kind of error has occurred. It's a `TypeError`, which tells us that it has something to do with converting between types or combining different types.\n", "\n", "The second two lines tell us where the error occurred. The `in ()` part tells us that the error is happening directly in the code that we are working on and `----> 1 print` tells us that it's occuring on the first line. This isn't particularly useful in this simple case, because we only have one line of code that doesn't call any external modules, but knowing where the error occurred is very useful when writing larger programs.\n", "\n", "Finally, the last line tells us exactly what went wrong. The error informs us that we cannot concatenate strings and integers, which is what we are trying to doing using the `+`." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Another common error occurs when you accidentally reference a variable that doesn't exist yet. For example," ] }, { "cell_type": "code", "collapsed": false, "input": [ "body_mass_kg = 25\n", "body_mass_g = bodymass_kg * 1000" ], "language": "python", "metadata": {}, "outputs": [ { "ename": "NameError", "evalue": "name 'bodymass_kg' is not defined", "output_type": "pyerr", "traceback": [ "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m\n\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[0mbody_mass_kg\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0;36m25\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m----> 2\u001b[0;31m \u001b[0mbody_mass_g\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mbodymass_kg\u001b[0m \u001b[0;34m*\u001b[0m \u001b[0;36m1000\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m", "\u001b[0;31mNameError\u001b[0m: name 'bodymass_kg' is not defined" ] } ], "prompt_number": 2 }, { "cell_type": "markdown", "metadata": {}, "source": [ "The error message tells us that:\n", "\n", "1. The error is a `NameError` so it has something to do with a variable name\n", "2. It occurs in line 2 of the code we are writing\n", "3. That we asked for a variable named `bodymass_kg` that doesn't exist yet\n", "\n", "The reason that `bodymass_kg` doesn't exist is because there is a typo in the name, it should be `body_mass_kg`. This is a very common type of error." ] }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "Read Error Messages Carefully" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "One of the most common mistakes that new programmers make is to see an error try to figure out what is wrong with their code without actually carefully reading the error message. Always taking a few seconds to read the error message will help you find the location of the error and understand how you should fix it much more quickly." ] } ], "metadata": {} } ] }