Dark theme

Practice


Ok, here's four files. See if you can fix the bugs in them. Try to fix them without looking at the answers (on the next page). Use the messages from Python and your knowledge of variables.

test3.py: This uses code you know; however, there is more than one bug in here to fix. You'll see new issues appear as you fix each.

test4.py: This is much more complicated, and probably includes code you won't recognise (you can come back to it at the end of the course!), but you should still be able to fix it because the bug will be familiar (obviously you shouldn't normally run code you don't understand on your machine, but here we want to show you that even complicated code can fail because of simple issues, and so you can see what happens when multiple issues are shown associated with one bug. Remember, it is the last message at the bottom that tells you most information).

test5.py: With this code, the code is typed correctly, but a problem arises at import time. Python won't find the library if you've just installed Anaconda. You can't fix this code without installing the library: don't worry about this now; we just wanted to show you what an import-time error looks like. If you delete the two ''' lines to uncomment the code, you'll also see what it looks like when Python gives you multiple different issues.

test6.py: Finally, here's a different kind of error. In this one, the code is typed correctly, but a problem arises as the code runs. Can you fix this? This is a tricky one; again, don't worry about the code you don't understand - again, we've added some complications so you can see what this looks like with mutiple messages. Concentrate on the line with the issue in it. What happens when we cast to an "int"? What could we cast to instead?

If you've fixed all those, congratulations, you're well on your way to understanding Python's error messages. If not, see the next page for brief answers.