Sometimes, you might see a traceback that is very long -- sometimes they might even be 20 levels deep!\n", "This can make it seem like something horrible happened,\n", "but really it just means that your program called many functions before it ran into the error.\n", "Most of the time,\n", "you can just pay attention to the bottom-most level,\n", "which is the actual place where the error occurred.
\n", "\n", "Some indentation errors are harder to spot than others.\n",
"In particular, mixing spaces and tabs can be difficult to spot\n",
"because they are both whitespace.\n",
"In the example below, the first two lines in the body of the function\n",
"some_function
are indented with tabs, while the third line — with spaces.\n",
"If you're working in a Jupyter notebook, be sure to copy and paste this example\n",
"rather than trying to type it in manually because Jupyter automatically replaces\n",
"tabs with spaces.
def some_function():\n", " msg = "hello, world!"\n", " print(msg)\n", " return msg\n", "
Visually it is impossible to spot the error.\n", "Fortunately, Python does not allow you to mix tabs and spaces.
\n", "File "<ipython-input-5-653b36fbcd41>", line 4\n", " return msg\n", " ^\n", "TabError: inconsistent use of tabs and spaces in indentation\n", "
Read the python code and the resulting traceback below, and answer the following questions:
\n", "# This code has an intentional error. Do not type it directly;\n", "# use it for reference to understand the error message below.\n", "def print_message(day):\n", " messages = {\n", " "monday": "Hello, world!",\n", " "tuesday": "Today is tuesday!",\n", " "wednesday": "It is the middle of the week.",\n", " "thursday": "Today is Donnerstag in German!",\n", " "friday": "Last day of the week!",\n", " "saturday": "Hooray for the weekend!",\n", " "sunday": "Aw, the weekend is almost over."\n", " }\n", " print(messages[day])\n", "\n", "def print_friday_message():\n", " print_message("Friday")\n", "\n", "print_friday_message()\n", "
---------------------------------------------------------------------------\n", "KeyError Traceback (most recent call last)\n", "<ipython-input-1-4be1945adbe2> in <module>()\n", " 14 print_message("Friday")\n", " 15\n", "---> 16 print_friday_message()\n", "\n", "<ipython-input-1-4be1945adbe2> in print_friday_message()\n", " 12\n", " 13 def print_friday_message():\n", "---> 14 print_message("Friday")\n", " 15\n", " 16 print_friday_message()\n", "\n", "<ipython-input-1-4be1945adbe2> in print_message(day)\n", " 9 "sunday": "Aw, the weekend is almost over."\n", " 10 }\n", "---> 11 print(messages[day])\n", " 12\n", " 13 def print_friday_message():\n", "\n", "KeyError: 'Friday'\n", "
print_message
KeyError
Friday
is not a key in messages
.SyntaxError
or an IndentationError
?def another_function\n", " print("Syntax errors are annoying.")\n", " print("But at least python tells us about them!")\n", " print("So they are usually not too hard to fix.")\n", "
SyntaxError
for missing ():
at end of first line,\n",
"ndentationError` for mismatch between second and third lines.\n",
"A fixed version is:
NameError
do you think this is?\n",
" In other words, is it a string with no quotes,\n",
" a misspelled variable,\n",
" or a variable that should have been defined but was not?for number in range(10):\n", " # use a if the number is a multiple of 3, otherwise use b\n", " if (Number % 3) == 0:\n", " message = message + a\n", " else:\n", " message = message + "b"\n", "print(message)\n", "
3 NameError
s for:
number
being misspelledmessage
not defineda
not being in quotes.Fixed version:
\n", "\n", "seasons = ['Spring', 'Summer', 'Fall', 'Winter']\n", "print('My favorite season is ', seasons[4])\n", "
IndexError
; the last entry is seasons[3]
, so seasons[4]
doesn't make sense.\n",
"A fixed version is: