BLACK FRIDAY EXTRA SAVINGS EVENT - EXTENDED
START FREE

Looking for Tips on processes to do when an error in the code is found

I was on this section of the Data Cleaning Course: https://app.dataquest.io/m/351/cleaning-and-preparing-data-in-python/8/parsing-numbers-from-complex-strings-part-two

I originally got stock on this portion of the code when I was asked to create a function that simply splits a date range:

 if '-' in date:
        
        # Got to remember to store the result of the method back into a variable (or it doesn't save)
        date = date.split('-')
        
        date_one = int(date[0])
        date_two = int(date[1])
        
        average = (date_one + date_two) / 2
        average = round(average)
        return average

The above came out correct, but originally I forgot to store the result of the method ‘str.split’ back into a variable and it caused what I think is called a ‘logical error’ or maybe an error in methods.

The way i worked through it was I used the print function during various parts of that for loop and realized the function was not ‘working’. So I just sort of said ‘what happens if I store it in a variable’ and it worked.

That being said, what is a common ‘checklist’ of methods you guys/gals use to trouble shoot your code before you look to google and after all else fails?

Thank you!! Hope your week is going well :slight_smile:

1 Like

Hi @burnsdillion,

Debugging is a skill that will keep on improving with practice. Since this is a skill that we naturally acquire as we improve our programming knowledge, everyone may have a different approach when it comes to debugging. However, Python errors are quite helpful if you can understand it. Here is an example:

def concat(*strings):
    return ' '.join(strings)

concat(1, "Debugging", "is", "Fun")
TypeErrorTraceback (most recent call last)
<ipython-input-1-fac8459d9d77> in <module>()
      3 
      4 
----> 5 concat(1, "Debugging", "is", "Fun")

<ipython-input-1-fac8459d9d77> in concat(*strings)
      1 def concat(*strings):
----> 2     return ' '.join(strings)
      3 
      4 
      5 concat(1, "Debugging", "is", "Fun")

TypeError: sequence item 0: expected str instance, int found

The arrows help us to drill down on the actual location of the error. Here the first arrow is pointing at line 5 because the concat function failed to complete. The last arrow points to the exact line of the error.

----> 2 return ' '.join(strings)

I will look at the last arrow first. Now let’s try to break down the error.

TypeError: sequence item 0: expected str instance, int found

If we try to describe this in plain English, then it would be.

“Hey, it appears that you are using an incompatible type for your operation. You may want to check out the first item of your sequence. Actually, I was expecting a string object instead of this integer.”
Interpreter

Here the sequence is our strings (argument list). Now to figure out what value I have passed as arguments, I have to check out the previous arrow.

----> 5 concat(1, "Debugging", "is", "Fun")

The interpreter was right. I am using an integer as the first argument.

If I couldn’t understand the error from the error message, then I would certainly do this:

I don’t follow a checklist. I believe the more errors you troubleshoot, you will know where you need to look to debug an issue.

Hope this helps :slightly_smiling_face:.

Best,
Sahil

1 Like