2.5 Implementing the Chatbot

Now we are ready to complete our chatbot program. Again, we will use the abstraction idea to build a function that will accept a question as a parameter and respond with an answer.

Recall the basic rules for the chatbot. It selects a response based on the first word in the question. But how can we test for the first word in the sentence? If we build a selection condition using the equality operator to compare the question to our “W” words, we would be testing the entire question. We want to test only if the question starts with the word. Fortunately, like the turtle data type, the string data type has methods. For now, we will look at one method that is particularly helpful here: startswith, which tests whether a ...

Get Python Programming in Context, 4th Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.