Let's Debug this Python Code for a Friend Now

Let's Debug this Python Code for a Friend Now

Here's a code I comprehensively debugged for a friend...Did I do it well?

"Simon, I've a problem with my code because it keeps giving me an error."

Hmm...

That was the call for help I got from a friend while working on an important project...Did I leave my project to answer?

Yes, I'll definitely do so...This friend is a beginner who just started learning how to code through Angela Yu's 100 Days of Coding. Thumbs up to Angela for creating a great course on Udemy.

What was the problem and how can you solve it?

Let's dive in now...

Here's the code written by my friend to determine a leap year...

year = input("Enter the year to check\n>>> ")   #line1
if year % 4 == 0:
    if year % 100 == 0:
        if year % 400 == 0:
            print("{} is a leap year".format(year))
    else:
        print("{} is not a leap year".format(year))
    else:
        print("{} is a leap year".format(year))
else:
    print("{} is not a leap year".format(year))

Just some questions before I show you how I debugged the code...

  1. Where you able to spot the errors in the code?
  2. What approach did you use in spotting these errors?

Good job if you spotted the errors to the coding challenge...

But..How did I debug the code?

First Step

Here's how I solved this problem....

line1 asks for an input from the user. The input keyword is used correctly. But...Recall that the input keyword accepts the string datatype...The variable year should be an int datatype.

Why an int datatype?

line2 shows that the variable year will be divided by an integer...Recall that an integer can't divide a string(a number can't divide an array of characters/letters in simple terms)

So, that means you'd have to change the datatype from line1

Now, the correct syntax for line1 would be...

#be careful with the amount of closing brackets to add
year = int(input("Enter the year to check\n>>> "))

But..."There's still an error in my code."

Hmm...Where can that be guys?

Oh!...I've spotted another error.

Second Step

year = int(input("Enter the year to check\n>>> "))
if year % 4 == 0:
    if year % 100 == 0:         #line3
        if year % 400 == 0:  #line4
            print("{} is a leap year".format(year))
    else:      #line6
        print("{} is not a leap year".format(year))
    else:      #line8
        print("{} is a leap year".format(year))
else:
    print("{} is not a leap year".format(year))

From line2 to the last line, you'd discover that's a block of nested if and else statements.

If the syntax for your if and else statements are correct, then the only problem causing the error should be an indentation

Look at the else statements written in line6 and line8 ; there seems to be a problem with the indentation.

Let's analyze the conditional statements to properly align the else statements

line6 should be rightly indented with the if statement block in line4.

why? The if statement in line3 has the else statement in line8

How do you avoid this problems with nested conditions?

Whenever you write the 1st if statement, also write the else statement, then begin to add your nested if statements.

Also, if any of the nested if statements has an else statement, write it before writing the next if statement

Debugged Code

year = int(input("Enter the year to check\n>>> "))
if year % 4 == 0:
    if year % 100 == 0:
        if year % 400 == 0:
            print("{} is a leap year".format(year))
        else:
            print("{} is not a leap year".format(year))
    else:
        print("{} is a leap year".format(year))
else:
    print("{} is not a leap year".format(year))
#Output
Enter the year to check
>>> 2020
2020 is a leap year


#Output
Enter the year to check
>>> 2021
2021 is not a leap year

Vital Tip

Did you ever think of the text editor or IDE(Integrated Development Environment used?

That's an important thing that would have definitely helped here...My friend was using the Jupyter Notebook environment which didn't highlight her indentation errors

An IDE like VSCODE which is my best IDE will immediately highlight the error and make it easier to debug.

Another interesting fact is that, VSCODE has a trace line that connects the if and else statement for a particular block. If you wrongly placed your else statement, you'll clearly see that your conditions aren't properly placed.

Leap year exercise.png

Here's a picture using Jupyter's notebook

vscode leap year screenshot.png

Here's a picture using VSCODE - The trace line of the conditional statements and the error underline of the else statement gives you the indication that something is wrong with the code.

Conclusion

There are different factors that makes it easy to debug the errors in your code...Putting these little combined factors I used in debugging the code, made it easier to find the errors.

I considered the datatype of the input and also recognized that there's a nested conditional statement block.

An additional help is using the right IDE that'll monitor your coding syntax like VSCODE

Thank you for debugging this code with me...

Let me know in the comment section if I comprehensively debugged the code for my friend who's a beginner.

Gracias!!!