Conditional Exercises
After completing the code-along activity, work on the exercises below.
One More Question
Building from the Choose Your Path project, add an additional question to make the game a little more involved.
- In the main.cs file, in the
Main
body, find the finalif
statement - Above that, use
Console.WriteLine
to ask another question -"You get to school. Do you stop at the water fountain?"
- Display the options:
" A. Yes"
/" B. No"
- Use
Console.Write
to print"> "
- Under that, create a new
string
variable namedchoice3
- Set the
choice3
variable toConsole.ReadLine()
- Use
Console.Clear()
to clear the console - Under that, create an
if
structureif
keyword- Parentheses (condition)
- Curly brackets (body)
- For the condition (between the parentheses), check if
choice3
equals"A"
- In the body (between the curly brackets), set the
health
variable tohealth + 1
- Also in the body of the
if
, display a message saying "You drink some water." - Under the
if
body (after the}
), create anelse if
structureelse if
keyword- Parentheses (condition)
- Curly brackets (body)
- For the
else if
condition, check ifchoice3
equals"B"
- In the body of the
else if
, set thehealth
variable tohealth - 1
- Also in the body of the
else if
, display a message saying "You do not drink water."
Run the program again, and verify that the third question properly impacts the game!
A New Project - Chat Bot
Using conditional statements, a whole lot of things are possible.
While the definition of "artificial intelligence" may not always include simple if
statements, it is certainly possible to create the illusion of intelligence using conditionals!
Create a program that can respond to a number of different chat messages.
Setup
Start by creating the new project.
- Create a new C# Repl project
- Name it "Chatbot"
- In the main.cs file, in the
Main
body, set the text color:Console.ForegroundColor = ConsoleColor.Black;
- Under that, set the background color:
Console.BackgroundColor = ConsoleColor.White;
- Under that, clear the console to make it a little cleaner:
Console.Clear();
- Next, use
Console.WriteLine
to display a message saying "Welcome to the Chatbot" - Under that, add an empty
Console.WriteLine
to make some extra space
Run the program, and verify that the message appears in the proper color.
A Question
Next, the program should ask the user a question. This can theoretically be any question.
- Under the lowest
Console.WriteLine
, create anotherConsole.WriteLine
- Display a message with a question, like "What do you want to say?"
- Under that, create a new
string
variable namedanswer
- Set the
answer
variable to be aConsole.ReadLine()
Run the program. Verify that the question appears, and it is possible to enter an answer!
A Response
Now that the user has entered an answer, it is possible to respond to that answer. The response should depend on what the user said. For example, if the user says "You talkin' to me?" the bot could say "Well I'm the only one here."
Use an if
statement to create that response!
- Under the
answer
variable, create anif
structureif
keyword- Parentheses (condition)
- Curly brackets (body)
- For the condition (between the parentheses), check if the
answer
variable equals"You talkin' to me?"
- In the body (between the curly brackets), add a
Console.WriteLine
statement - Make the
WriteLine
display a message saying"Well I'm the only one here."
Run the program again. Verify that entering the exact answer "You talkin' to me?" will make the program display the correct response!
The code should look something like this:
if (answer == "You talkin' to me?")
{
Console.WriteLine("Well I'm the only one here.");
}
Other Responses
Using the same type of structure, it's possible to make the program respond to any message at all! Add some more if
statements, each of them checking the answer
value and printing a different response based on the answer. Feel free to customize the responses, or use the ones from this table:
answer |
Response |
---|---|
"You killed my father." | "No, I am your father!" |
"I'm going to Mordor alone." | "Of course you are, and I'm coming with you!" |
"You look like my grandpa." | "Oh, is your grandpa super cool?" |
"Your grandpa invented baseball?" | "Did I say baseball? I meant spray-on eyebrows." |
"Is this the Krusty Krab?" | "No, this is Patrick." |
"This is the worst day of my life." | "The worst day of your life so far." |
Dynamic Colors
Instead of simply setting the colors at the beginning of the program, ask the user which colors to use!
- At the top of the
Main
body, above theConsole.ForegroundColor
, make a new space - There, use
Console.WriteLine
to display a message: "Which text color?" - Under that, create a new
string
variable namedcolorAnswer
- Set the
colorAnswer
variable to beConsole.ReadLine()
- Under that, create an
if
structureif
keyword- Parentheses
- Curly brackets
- For the condition (between the parentheses), check if
colorAnswer
isRed
- In the body (between the curly brackets), use
Console.ForegroundColor = ConsoleColor.Red
to set the color
Run the program, and verify that it is possible to select "Red" as the text color!
Add additional if
structures for some additional colors, like ConsoleColor.Green
, ConsoleColor.Cyan
, and ConsoleColor.Yellow
.
Challenges
After completing these exercises, start working on the challenges.