15: Error handling in python

Debug it

Make your code cope when things go wrong
Error handling: Make your code cope when things go wrong

Below is some code that has been deliberately broken so that you can fix it. 

The code is supposed to generate a random times tables quiz question and test you to see if you get the right answer:

Challenges

  1. Fix all syntax errors

    Hint: A syntax error means the code has broken the rules of the language. Work upwards from the line number in the error message to see what’s wrong or what’s missing.

  2. Use try & except to display an error message instead of crashing if the user types in potato

    Hint: find the line that will crash if you enter potato and surround it with try and except

  3. Fix the logic error

    Hint: At the moment the program tells you you got the question right when you got it wrong and vice versa.

On the next page you’ll get some ideas for projects where you can use exception handling to create your own projects

Leave a Reply

Your email address will not be published. Required fields are marked *