Debug it
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
- 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.
- Use
try
&except
to display an error message instead of crashing if the user types inpotato
Hint: find the line that will crash if you enter
potato
and surround it withtry
andexcept
- 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
Page 1: Intro
Page 2: The theory: learn what you need to know as fast as possible.
Page 3: Try it: try out and adapt some working python code snippets.
Page 4: Debug it: Learn how to find and fix common mistakes.
Page 5: Extend it: Choose a project idea to use your newfound python skills.