Python For Beginners

Python For Beginners

Breaking and bug-fixing in Python

In this lesson we will learn how to break our program.

More importantly, we will learn how to recognize what is broken and how to fix it.

This is a serious matter.

If you continue your goal of becoming a developer you will break programs a lot. You will break them while you learn, while you work as a junior developer, while you work as a senior developer. You will break them in testing environment, in production and in your dreams as well.

Yup, simply put, but let us use a proper terminology here:

  • Bug – an error in code
  • Debugging – process of finding a bug (there are different approaches here, more on that later)
  • Error log – list of the error messages
  • Stack trace – Message which is returned by compiler when program is stopped because of error
  • Syntax bug – an error which was introduced because you coded something wrong (i.e. you entered IFF instead of IF)
  • Semantic bug – and error in logic. Meaning, program is coded correctly, but logic is not good, so the end result is not good as well.

Hints:

  1. Syntax bugs are easier to find and fix, because they will stop your program from execution.
  2. Semantic bugs are harder to find, because everything works but the logic is wrong.
  3. At the beginning of your journey, you will make lots of syntax bugs (which is ok).
  4. In time, syntax bugs will go away, and they will be replaced with semantic bugs.

But let’s back to what we were doing, breaking our first program.

Just as a reminder, at the previous lesson, we made a program which is determining if number is bigger or smaller than zero.

x = int(input("Please enter an integer: "))

if x < 0:
    print('The number you have entered is negative')
elif x == 0:
    print('The number you have entered equals zero')
else:
    print('The number you have entered is positive')

Then, we executed this program in https://www.onlinegdb.com/online_python_compiler  and everything worked properly.

Now, let’s introduce some syntax bugs and see what will happen.

First bug: At the line 5, we want to change == with = to make it look like ‘elif x = 0:’

  • In programming there is a difference between == and =.
  • Two equal signs check if the items are the same, example x == 5 (this checks IF x is equal 5)
  • One equal sign assigns value to a variable, example x = 5 (this means that 5 is assigned to x)
  • Essentially, this will be a bug, because in IF-ELIF-ELSE structure we need checks ‘==’ and not assignment ‘=’
bug1 error log
bug1 error log

This is a message we will get after executing ‘buggy example’. Python compiler will tell us that there is a “SyntaxError” and point out to the problematic line. However, compiler will not tell us how to resolve it. In our case, it is simple. Since we introduced a bug intentionally, we can simply solve it by using ‘==’ instead of ‘=’.

Whole point of this example is to intentionally break something, so we can see what will the compiler complain about. Later, when we make a real mistake (this one or similar) error messages will not be so strange for us.

Second bug: At the line 3, we will change ‘if x < 0:’ with ‘if x < 10:’

  • This is not a syntax bug, because syntax will still be correct, program will work.
  • This is a semantic bug, because first IF will not calculate things correctly.
  • Result will be that numbers smaller than 10 will be recognized as negative (i.e. 5 will be reported as negative).
  • As you can see, this is a logical bug, which is easy to detect here, but somewhere else (in bigger code) it would cause issues.

If you run this example, result will be like this

semantic bug 7 reported as negative
semantic bug 7 reported as negative

That is it for this lesson. We broke our program two times and fixed it.

First time it was a syntax bug and second time it was a semantic bug.

There will be a dedicated lessons on debugging, but for now this will be sufficient.

 

Homework:

  • Play with this code, break it intentionally.
  • Change some lines and observe what will be reported as error in console.
  • Try to make semantic bug as well, not only the syntax ones.

 

Leave a Comment