At this point, you've learned how to enable your chatbots to make decisions; they can decide how to respond to you based on what you wrote to them. A vital component of intelligence is remembering and learning. In this section, you'll learn to give your chatbots the ability to remember things.
In an earlier part of this tutorial, you created variables that held strings. These variables were nice, but once your bot returned a message the value stored in the variable could no longer be accessed. In this section, you'll use a global variable called "state" to allow your chatbot to store a value that will stay around. The global variable's value can be called like a regular variable's value. The variable "state" is a string so it can store words or sentences.
Note that the global variable "state" has an initial string equal to "0". This is important for checking if you are in the starting state. If the state is not "0", then you know you have changed your state.
Consider the following example:
state = message;
In the preceeding example, we assigned the parameter variable message to the global variable "state". This changes state's initial value of "0" to whatever the message parameter was.
Try creating a chatbot that will respond to the user with the message that they wrote last time. This is similar to BouncerBot, except that instead of replying with the message just written by the user, the bot will store the message written by the user last time and reply with that. If you get stuck or if you want to verify your code, look at the LastMessageBouncerBot Solution.
What is state? If you're sitting at a computer, part of your state is just that - sitting at a computer. But that's just one component of your state. You're also awake, a person, tired, happy, wondering when this list will stop so you can get back to coding chatbots. All of these provide information about your current condition, your state. Information like this is handy to have around when making intelligent decisions. For instance, if my state includes the fact that I'm standing beside a cliff, I'm better able to make an informed decision about whether to start practicing my spinning long jump. Without that information, I might end up a pancake on a canyon floor.
In a conversation, it's similarly handy for your chatbot to remember what it's said so it can expect the responses that it gets. If someone writes the message "three" to your chatbot out of the blue, it might respond with a generic greeting like "Hello random person". If your chatbot had remembered that it had just asked them how many chocolate sundaes they ate at dinner last night, however, it could have replied with something much more intelligent, like "Wow, that's a lot of ice cream!" Remembering relevant state information will help your chatbots seem more intelligent and now you have the tools to give this capability.
Write a chatbot starts off by asking "How are you doing?" but responds "Glad to hear it." if it's expecting to hear how someone's doing. If you get stuck, view the HowAreYouDoingBot Solution.
Nested conditionals are conditional statements that appear within the blocks of code defined by other conditionals. Computer scientists say they "nest," just like sets of parentheses are said to nest within one another in a mathematical expression like (6*(3+4))/7. Nested conditionals are handy to use when you're keeping track of state because often you'll want to first check on a feature in your state and then decide how to respond. For instance, a chatbot could check to see if it's in a state that's expecting the user to respond with how many ice cream sundaes they ate last night and then choose how to respond based on what they just said. The following example, does just that:
if(state == "asked about ice cream") { state = "reset the state"; if(message == "0") { return "None? That's crazy!"; } else if(message == "1") { return "One is a good number. Everything should be done in moderation."; } else { return "Wow, that's a lot!"; } } else { state = "asked about ice cream"; return "How many ice cream sundaes did you eat last night at dinner?"; }
In this example, the chatbot first checks to see if the variable "state" is the string "asked about ice cream". If it is, then the chatbot starts executing the code in the block of code associated with the "if". In the "if" block, it first resets state by setting it equal to the string "reset the state". This will prevent the bot from thinking that it just asked about ice cream the next time it receives a message. It then enters the nested conditional to decide what response it should send back to the user.
But how did the state variable get set to "asked about ice cream" in the first place? That happens in the "else" block of the outer conditional that tests what state we're in. If "state" is not equal to "asked about ice cream" then it executes the following two lines of code located in the "else" block:
state = "asked about ice cream"; return "How many ice cream sundaes did you eat last night at dinner?";
The first line sets the state to "asked about ice cream" so that they next time the bot has to respond to a message, it will know that it just asked about ice cream. Then it actually asks the question. It's important that state is set before returning the message because otherwise that statement won't get executed and the chatbot will not remember its state.
Now try creating your own chatbot that uses state and nested conditionals. Expand your HowAreYouDoingBot from above to choose among several responses after it receives a reply to how the user is doing. For example, if the user replied "good" your chatbot can respond "Glad to hear it." If they say bad, your chatbot could return an empathetic response like "Oh, I'm so sorry to hear that." (as all good chatbots should). You might even want to try adding in regular expression matching for more flexible matching of user responses. Feel free to personalize your chatbot to make it extra happy, mean, empathetic, apathetic, rude, etc. While there are many acceptable solutions and variations to this exercise, if you get stuck view the NestedConditionalsHowAreYouDoing Solution.
Previous Next