Python Debugging Exercise 2

Python Debugging Exercise 2

Fun problem solving python exercise for all levels.

Most developers advice that another way of learning to become a better software engineer is reading lines of code to debug.

On that note, I've a challenge for you with a code I want you and I to debug together.

If you're fired up, then let's jump right into today's code debugging exercise.

Today's exercise has to do with a game called “Animal crackers.”

This is a game from Jose Portilla's Python Bootcamp exercises.

What's the exercise all about?

You enter a two word string or text and the function checks if the words begin with the same letter.

Does that sound fun to write and debug?

Here's the code below.

def animal_crackers(text):
    '''Takes a two word string and return
    True if both words begin with the same letter'''

    text_1 = text.split()

    word_1 = text_1[0]
    word_2 = text_1[1]

    if word_1[0] == word_2[0]:
        return True
    return False

print(animal_crackers('Levelheaded llama'))
print(animal_crackers('Crazy Kangaroo'))

STEPS INVOLVED IN THIS CODE

Step1: Define your function using any of the variable naming casing in python. You might want to check out the Pep-8 styling rules. Ensure that the variable name is readable to other developers or anyone reading your code.

Step2: Write a documentation(doc strings). This tells other developers or anyone what your function does. In future, someone might want to also use your code and you wouldn't want to write for yourself alone.

Step3: Use the split method to separate your text into two separate words. The output of the split method is a LIST.

Step4: Set the two words in the list to another variable name using their index.

Step5: Get the index of the first letters you need since you're comparing to check if the letters are the same.

Step6: Using the conditional if-else statement to return True if they're the same and false if the condition fails.

Step7: Call and also pass in the argument to the function.

Step8: Click run to see the output of the code.

OUTPUT1: FALSE || OUTPUT2: FALSE

But wait!

There seems to be a problem in the output. Output1 has the same letter in the argument sent to the function but returns false.

Hmm…Have you spotted the problem devs?

Oh!…Forgot to add a line of code that takes care of the lowercase and uppercase input from the user.

To solve the bug, I can either use a lowercase or uppercase function.

Let's fix that bug now devs!!!

def animal_crackers(text):
    '''Takes a two word string and returns
    True if both words begin with the same letter'''

    text = text.lower()
    text_1 = text.split()

    word_1 = text_1[0]
    word_2 = text_1[1]

    if word_1[0] == word_2[0]:
        return True
    return False

print(animal_crackers('Levelheaded llama'))
print(animal_crackers('Crazy Kangaroo'))

Let's click Run once again… OUTPUT1: TRUE || OUTPUT2: FALSE

Yes!!!…

You solved the problem with me and the bug got fixed. Great work guys and get ready soon for another mind-blowing python exercise.

CONCLUSION

This python debugging session helped you to remember some important functions and also taking note of a user's input when writing your codes.

If you missed the first debugging session, then here's a link to the article...

Python Debugging Session 1

See you soon on another debugging session!