Chapter 5 Activity Instructions

Chapter Five: Finding and Fixing Problems

Activity: Chat-Bot

 

ChatBot-1024In this chapter, you learned several ways to find and fix problems in your code. You might conduct a code review, or add program tracestatements, or even use a debugger to step through the program line-by-line.

We have created a program called "ChatBot" that should have a short, interactive conversation with the user. Unfortunately, we didn't get all of the code completely right, and there are several errors that we need help finding and fixing. Your job is to make the ChatBot code work exactly according to the program requirements.

ChatBot should ask for the user's name, then continue with a couple of other questions. The third question and response will depend on the user's answer to the second question.

Specific Questions and Outputs

ChatBot's specific questions and response are described below. There are 8 possible paths through the code, depending on the user's answers to the three questions.

Question and Response #1

ChatBot should first ask the following question and get store the response as a string in the name variable:

"What is your name?"

ChatBot should then print

"Hi <name>, nice to meet you. I am Chatbot-1024."

Question #2

ChatBot should next ask the following question and store the input() response as a string in the sport variable:

"What is your favorite sport?"

ChatBot will then check the sport variable with a series of if / elif statements to see if it equals "football", "baseball" or "basketball".

Question and Response #3

If the sport answer is "football", ChatBot will then use input() to ask Question #3 and store the response as an integer in the yards variable:

"I like football too. Can you tell me the length of a football field in yards?"

Response #3 is printed by checking the user's yards answer:

If the yards is less than 100, ChatBot should print "No, too short."

ElIf the yards is greater than 100, ChatBot should print "No, too long."

Else, ChatBot should print "That's right!"

Elif the sport answer is "baseball", ChatBot will then use input() to ask Question #3 and store the response as an integer in the strikes variable:

"I play baseball every summer. How many 'strikes' does it take to get a batter out?"

Response #3 is printed by checking the user's strikes answer:

If the strikes is equal to 3, ChatBot should print "Yes, 1, 2, 3 strikes you're out..."

Else, ChatBot should print "Actually, 3 strikes will get a batter out."

Elif the sport answer is "basketball", ChatBot will then use input() to ask Question #3 and store the response as a string in the team variable:

"The Harlem Globetrotters are the best. Do you know the name of the team they always beat?"

Response #3 is printed by checking the user's team answer:

If the team is equal to "Washington Generals", ChatBot should print "Yes, those Generals can never catch a break."

Else, ChatBot should print "I think it's the Washington Generals."

Else

ChatBot should print "That sounds cool; I've never played <sport>."

 

Starting Code

Here is the starting ChatBot program. If you are working offline, you can copy and paste it into a "ChatBot.py" source file that you create. But if you are working with CompuScholar's auto-graded system, you will find this code waiting for you when you click on the activity link in the course interface.

# Student Name

# Start the Conversation
name = input("What is your name? )
print("Hi " + name + ", nice to meet you.  I am Chatbot-1024.")

# ask about a favorite sport
sport = input("What is your favorite sport? ")
if ((sport == "football") or (sport == "Football")):
  # respond to football with a question
  yards = input("I like football too.  Can you tell me the length of a football field in yards? ")
  # verify and comment on the answer
  if (yards < 100):
     print("No, too short.")
  elif (yards < 100):
     print("No, too long.")
  else:
     print("That's right!")

elif ((sport == "baseball") or (sport == "Baseball")):
  # respond to baseball with a question
  strikes = int(input("I play baseball every summer.  How many 'strikes' does it take to get a batter out? "))
  # verify and comment on the answer
  if (strikes == 3):
     print("Yes, 1, 2, 3 strikes you're out..."
  else:
     print("Actually, 3 strikes will get a batter out.")

elif ((sport == "basketball") and (sport == "Basketball")):
  # respond to basketball with a question
  team = input("The Harlem Globetrotters are the best.  Do you know the name of the team they always beat? ")
  # verify and comment on the answer (check for either version with or without capitals)
  if ((team == "washington generals") or (team == "Washington Generals")):
     print("Yes, those Generals can never catch a break.")
  else:
     print("I think it's the Washington Generals.")

else:
  # user entered a sport we don't recognize, so just display a standard comment
  print("That sounds cool; I've never played " + sport + ".")

print("Great chatting with you.")

 

Activity Results

Your job is to find and fix all of the ChatBot errors, so all 8 test cases below will pass. You can use any combination of code review, program tracing or the debugger that you like to get the job done.

Hint: There are 2 syntax errors, 2 run-time logical errors and 1 run-time exception that you need to fix.

 

Test #1 - Input: Name = Matt, sport = "football", length = 100

What is your name? Matt
Hi Matt, nice to meet you. I am Chatbot-1024.
What is your favorite sport? football
I like football too. Can you tell me the length of a football field in yards? 100
That's right!
Great chatting with you.

Test #2 - Input: Name = Matt, sport = "football", length = 75

What is your name? Matt
Hi Matt, nice to meet you. I am Chatbot-1024.
What is your favorite sport? football
I like football too. Can you tell me the length of a football field in yards? 75
No, too short.
Great chatting with you.

Test #3 - Input: Name = Matt, sport = "football", length = 125

What is your name? Matt
Hi Matt, nice to meet you. I am Chatbot-1024.
What is your favorite sport? football
I like football too. Can you tell me the length of a football field in yards? 125
No, too long.
Great chatting with you.

Test #4 - Input: Name = Monica, sport = "baseball", strikes = 3

What is your name? Monica
Hi Monica, nice to meet you. I am Chatbot-1024.
What is your favorite sport? baseball
I play baseball every summer. How many 'strikes' does it take to get a batter out? 3
Yes, 1, 2, 3 strikes you're out...
Great chatting with you.

Test #5 - Input: Name = Monica, sport = "baseball", strikes = 4

What is your name? Monica
Hi Monica, nice to meet you. I am Chatbot-1024.
What is your favorite sport? baseball
I play baseball every summer. How many 'strikes' does it take to get a batter out? 4
Actually, 3 strikes will get a batter out.
Great chatting with you.

Test #6 - Input: Name = Camila, sport = "basketball", team = "Washington Generals"

What is your name? Camila
Hi Camila, nice to meet you. I am Chatbot-1024.
What is your favorite sport? basketball
The Harlem Globetrotters are the best. Do you know the name of the team they always beat? Washington Generals
Yes, those Generals can never catch a break.
Great chatting with you.

Test #7 - Input: Name = Camila, sport = "basketball", team = "Patsies"

What is your name? Camila
Hi Camila, nice to meet you. I am Chatbot-1024.
What is your favorite sport? basketball
The Harlem Globetrotters are the best. Do you know the name of the team they always beat? Patsies
I think it's the Washington Generals.
Great chatting with you.

Test #8 - Input: Name = Frederick, sport = "lacrosse"

What is your name? Frederick
Hi Frederick, nice to meet you. I am Chatbot-1024.
What is your favorite sport? lacrosse
That sounds cool; I've never played lacrosse.
Great chatting with you.

When your program is complete and tested, submit it for grading!

Activity Rubric

If your project is being automatically graded by our system, your grade will be calculated from 0 to 100 as follows:

PointsDescription
8Syntax Error #1 fixed
8Syntax Error #2 fixed
8Run-time Error #1 fixed
8Run-time Error #2 fixed
8Run-time Exception #1 fixed
8Test Case #1 passes
8Test Case #2 passes
8Test Case #3 passes
8Test Case #4 passes
7Test Case #5 passes
7Test Case #6 passes
7Test Case #7 passes
7Test Case #8 passes

 

End of Activity


Last modified: Wednesday, 8 May 2019, 8:52 AM