Recover from Errors
Let’s have a look at some common bugs and how to avoid them.
In this lesson, we’ll look at some of the common errors that we as programmers make and how to watch out for them. So, let’s begin!
Bugs
Following proper syntax and sequence is imperative while writing codes. But we all make mistakes, and it is not uncommon to run into errors while we code. The errors and flaws in code and its functionality are called bugs.
The two most common types of bugs are syntax errors and logical errors.
Syntax errors
Syntax errors occur when the code does not conform to the syntax rules of the programming language. These errors prevent the program from running. The code below provides an example of a syntax error in our code.
# Initialize a variable 'full_name' with an empty string full_name = "" # Prompt the user to enter their first name and store it in the 'first_name' variable first_name = input("Enter your first name: )" # Prompt the user to enter their last name and store it in the 'last_name' variable last_name = input("Enter your last name: ") # Concatenate the names with a space in between and store the result in 'my_name' full_name = first_name + " " + last_name # The `+` symbol can be used to combine two strings. # Display a message with the user's full name using the 'full_name' variable print("My name is", full_name)
Note: The line number with the error in the output above is a bit misleading. Study the code line-by-line and identify the bug.
Find the bug
Which line in the code has a syntax mistake?
Line 7
Line 11
Line 5
Line 2
Line 8
If we fix the error, the code will run just fine.
Logical errors
These are more subtle and challenging to detect. Unlike syntax ...