10: Logic Errors in Python

Try it:

Find and fix common errors in your code
Logic errors in python: Find and fix common errors in your code

Below you’ll see some example python code that attempts to calculate the area and perimeter of a rectangle.

Logic errors in the code mean that whilst the program does exactly what it’s told to do without crashing, it doesn’t do what it’s supposed to do (it gets the calculations wrong)

Press Ctrl + Enter  to run the code.

This code will run without crashing but it doesn’t calculate the correct results.

The area of a rectangle should be the width multiplied by the height.

The perimeter of a rectangle should be the sum of the width, the height, the width again and the height again in order to calculate the distance all the way around the edge.


Start by running the code and testing it with a variety of different answers to the questions.

  1. Fix the logic error on line 10 when displaying the perimeter

    The code has been told to display the area instead of the perimeter. It won’t crash but it will do what the code says rather than what we want it it to do. That’s a logic error and needs changing.

  2. Fix the logic error on line 9 when calculating the perimeter

    The perimeter of a rectangle means the distance you’d have to travel to go all the way around the edge. It should be the width plus the height plus the width again plus the height again

  3. Fix the logic error on line 12 when checking if the shape is square

    A rectangle is a square if the height and width are the same length. Line 12 has got this the wrong way round and needs to use == rather than != to mean equal to rather than not equal to

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


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.

4 thoughts on “10: Logic Errors in Python

  • February 26, 2019 at 10:33 am

    Shocking website!! I could not get the output without changing the browser which is not what a website should do!

    • February 26, 2019 at 11:38 am

      Shocking feedback!! Please could you let me know what browser you were using and what problem you had so I can improve the shocking website painstakingly put together for free in my spare time. It’s a long way from perfect. Alternatives are available.

  • March 26, 2020 at 3:53 pm

    Hiya, great website, the pound sign(£) creates a unicode error and the code will not run. If you delete the £’s it runs fine. I have tried this in Edge and Chrome.

    • March 26, 2020 at 8:34 pm


      Thanks so much for getting in touch and reporting that bug. It should now be fixed – all strings should support unicode characters. Sorry about that!

      All the best 🙂


Leave a Reply

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