Homework: String Comparisons

Read section 8.10 in the book and do the examples. Let's encapsulate that into a function:

def compare_to_banana(word):
    """Prints a message about how a word compares with "banana"."""
    if word < 'banana':
        print("Your word, " + word + ", comes before banana.")
    elif word > 'banana':
        print("Your word, " + word + ", comes after banana.")
    else:
        print("All right, bananas!")
        

Test it!

>>> compare_to_banana("Pineapple")
Your word, Pineapple, comes before banana.
        

If you want to ignore the case of the letters, we can add a line to change that using a string method:

def compare_to_banana(word):
    """Prints a message about how a word compares with "banana"."""
    word = word.lower() #converts word to all lower case
    if word < 'banana':
        print("Your word, " + word + ", comes before banana.")
    elif word > 'banana':
        print("Your word, " + word + ", comes after banana.")
    else:
        print("All right, bananas!")
        
>>> compare_to_banana("Pineapple")
Your word, pineapple, comes after banana.
        

As always, I highly recommend going over the debugging section, 8.11.