We are going to create a simple chat bot, using the Console in DotNetFiddle or VSCode and a new command:
Feel free to personalize the opening message and greeting. Can you add a second prompt? Maybe a follow-up question? (Typing anywhere in the code window will also reset the console output).
To create a good list of responses to user input, it’s helpful to use a
Dictionary, which is a new .NET Type, similar to the
List, but with a matched pair of variables (
value). Don’t forget to import
Here are some challenges for you
- Add more commands and responses to the dictionary.
- Can you make the program smart enough to understand both capitalized and uncapitalized strings? (hint: check for a method on the input variable by typing a period after and seeing what pops up)
- Use a for loop to have the program ask for commands more than once.
- Can you figure out a way to prevent the error message if someone types in an unknown command?
Don’t peek ahead until you’ve at least tried to solve #3-4!
If you succeeded at creating a for loop for challenge #3 above, it probably looked something like this:
This saves us from writing out the code five times, but what if we want to keep going as long as the person is interested? That’s what a
while loop is for. While loops continue as long as a certain variable or formula is true.
We’ll also take this time to introduce a new kind of variable, the
field. The only difference between a regular variable and a field is that fields are created and maintained at the class level, so they are accessible from any method in that class.
No one likes seeing error codes pop up when running a program! To avoid the error that occurs when someone types in random commands, we need to understand why it is happening. In this instance, we have sent an
input as a key to our
Dictionary. The dictionary looks, but finds that key doesn’t exist. It doesn’t know what we want to do, so it throws an exception. Notice that even with the
while loop code, the program terminates at the exception.
There are several ways to handle this problem. First, we can check to see if the key exists ahead of time. Luckily, the
Dictionary class has a method for that purpose, called
ContainsKey, that returns a boolean value to tell you whether or not to proceed.
The other approach is to use a
Try/Catch block. The advantage of this approach is that we don’t need to check ahead, but we can still continue running if an exception is thrown. You can choose whether to print the exception, log it, ignore it, or have it trigger some other code. If there is no exception, the catch block is skipped.
By now, hopefully you have lots of ideas of what your chat bot could do. Make sure to save your work! You can go to DotNetFiddle.Net and create a free account to save your fiddles.
Of course, if you are using VS Code, the files are already saved on your machine. If you want to share your local code, you should learn about using Git and GitHub with VSCode. Github also allows you to accept pull requests, or code contributions from other programmers.
Want to share your ideas with us? From DotNetFiddle or GitHub, you can simply copy the address of your code and paste it in a comment on this page! Let us know what your chatbot can do.