python /A+ Computer Science/Magpie Chatbot Activity 2 Worksheet

 

python /A+ Computer Science/Magpie Chatbot Activity 2 Worksheet

Run Magpiev2.py and answer the questions below.

Part One Lab Questions:: How does it respond to:

1. My brother is my mother’s son.
getResponse returns:

2. I said no!
getResponse returns:

3. What are you doing today?
getResponse returns:

4. Nothing that my sister does is ever noticed.
getResponse returns:
Part Two Lab Questions:: Look at the getResponse method and the cascading elif statements.

1. What happens when more than one keyword appears in an input string?

2. Will the order in which the keywords are tested in the cascading elif statements change the output? Why or why not?

3. Explain how the tests for keywords should be prioritized in the getResponse method.

4. Will testing the keyword “no” last in the getResponse method result in the chatbot responding appropriately to all user input? Explain. (Consider statements like: I like pizza smothered in cheese)

part 1: answer the questions on part 1 of the worksheet.
”’
”’
getResponse(statement):
Magpie will respond appropriately based on keywords given.
precondition: statement is a valid string inputted at the keyboard.
postcondition: Magpie responds based on the if statments.

In the lab, you are to modify this method in part 2.
”’

def getResponse(statement):

response = “”;

if statement.find(“no”)>= 0:
response = “Why so negative?”

elif (statement.find(“mother”) >= 0 or
statement.find(“father”) >= 0 or
statement.find(“brother”) >= 0 or
statement.find(“sister”) >= 0):

response = “Tell me more about your family.”

else:

response = getRandomResponse()

return response

”’
getRandomResponse():
Magpie will return a random statement if a keyword is not found.
First it generates a random number
…and then chooses a statement based on that number, using a dictionary.

READ ALSO :   Case study - Managerial Report under linear programing chapter in Quantitative Methods for Business

In the lab, you are to modify this method in part 3.
”’
def getRandomResponse():

randomResponses = { 1: “Interesting, tell me more”,
2: “Hmmm.”,
3: “Do you really think so?”,
4: “You don’t say.”
}
whichResponse = randomResponses[randint(1,4)]

return whichResponse

”’
editing code below this comment may:
*cause the program to not work properly
*void the warranty
*destroy the universe.

You have been warned.

For the curious:

“while True” causes an infinite loop. THIS IS INTENTIONAL.
Inside the while loop, the computer feeds your input to the getResponse method,
which allows the chatbot to respond back to you.

Python keeps the chatbot going as long as you want to talk to it.
To quit the bot, just close the window and the program will terminate.
”’
response = input(“Hello, let’s talk.\n”)

while True:

response = input(getResponse(response) + “\n”)