From the course: Programming Foundations: Beyond the Fundamentals

Solution: Debugging

(upbeat music) - Hope you had fun with that challenge. You might have had questions while doing it or you might have had a moment when you weren't sure about what to do next. If so, that's totally normal. Understanding and working with programming concepts takes practice. In fact, even experienced developers run into errors they can't easily fix. Trying your hand at this challenge is the first step in building those muscles. When I open the file, I notice right off the bat that I have a red squiggle on line two, and moving the pointer over that, the linter tells me I have invalid syntax, but if I run it, the terminal includes a caret that points out the location in the line where it found the problem, which is the equal sign. And that's enough to remind me that I need a double equal sign for comparison. So I'm going to add in another equal sign here. I'm going to save that, and now my syntax error is gone. That red squiggle is no more. But now, I have a red squiggle on line nine, and when I hover over that, the linter error is undefined variable. Now, if I look up at the start of my code, the name of the function I'm calling is plant_recommendation, not plant_rec. So I can fix that, even using auto completion. So that's my runtime error fixed. I save and the IDE doesn't flag anything else. So I'll run my code in the terminal. I have three test cases in my file, but only two values are printed to the terminal, aloe and pothos. The third case, with the value high, should trigger the second elif statement. So I'll look at that statement more carefully, and I noticed that I repeated medium as the value for checking, rather than high. This is a logic error. So in that second elif statement, I'll replace medium with high and I'll save my code. I'll clear my terminal and I'll run one more time. And now, I see all three possible results printed in the order I expect. You may have found all the bugs right away or you may have taken a while or not found them all. That's okay. Debugging takes work and bugs aren't always obvious. Fortunately, debugging is a skill that you get better at the more you do it. And there's never any shortage of bugs when it comes to programming. Approach debugging with curiosity and a sense of adventure, and you'll continue to level up on your programming skills.

Contents