07: Python: Runtime errors

Extend it:

Find and fix common errors in your code
Python runtime errors: Find and fix common errors in your code

Now that you’re aware of what runtime errors are and how to use try and except to let your program cope with them without crashing, it’d be a good idea to go back to some of your previous python projects to make them more robust.

Challenges:

 
  1. Adapt this program which calculates some stats about how much you use your smartphone to make it more robust. 

    First, test the code to see what input you can put in which would make it crash. Then use try and except to give the user a friendly error message instead of crashing.

  2. Adapt this program which calculates how old you’ll be at any point in the future to make it more robust

    There’s some syntax errors to debug first before you can think about dealing with the runtime errors with try and except

  3. Write a take-away meal price splitter program which asks you how much a meal costs and how many people are going to share it. It will then work out how much each person has to pay.

    Remember that converting user input to a number is a time when you need to be careful about runtime errors. Use try and except to make your code more robust

3 thoughts on “07: Python: Runtime errors

  • Pingback: Python for beginners: free interactive self marking activities

  • October 27, 2018 at 8:39 pm
    Permalink

    For some reason when I save my code it doesn’t give me a new url to use, do you know why this is?

    Reply
    • October 27, 2018 at 9:02 pm
      Permalink

      Hi Lucas. Sorry it’s not giving you a new url.
      When you change the code in one of these tracked activities it will automatically save your changes just on your computer in a cookie so that if you refresh your page you don’t have to start all over again. If you want a new URL you’ll have to press Ctrl+S. You may have to tick the ‘I’m not a robot’ box if that comes up on screen. This is irritating but it limits people exploiting the fact that anyone can save without having to log in. If you’re still having problems, reply here and I’ll email you.

      Reply

Leave a Reply