Try it:

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

Below you’ll see some example python code which asks the user to choose how large a square should be drawn. If the user doesn’t type in a number it will display a friendly message rather than crashing:

Press Ctrl + Enter  to run the code.

Challenges:

Start by running the code.

  1. Change the default size from 0 to 10

    Line 6 will set the default shape size. If the user types in an invalid size (e.g. '' or 'zero') line 10 will not finish running because an exception will occur

  2. Change the error message to Invalid shape size

    Line 12 tells python what to do if an error occurs.

On the next page you’ll get some code with both syntax and logic errors

KPRIDE

KPRIDE stands for Keywords, Predict, Run, Investigate, Debug and Extend and it’s a way of helping you explore and understand python code. Click on the image below for a set of KPRIDE activities for this python skill.

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.