CSC 111: Intro to Computer Science through Programming

Homework 3

Due: Tuesday, Feb. 21 at 11:59pm on Moodle

The goal of this homework is to put together several concepts we have been learning during the past few weeks: conditionals and Boolean variables (if/elif/else statements), string methods, lists, random numbers, and for-loops. For this assignment, you are welcome to discuss your work with other students, but all code you submit should be your own original work. It should be produced and understood by you, without looking at anyone else's assignment.

Credit: modified from materials by Nick Howe

Quiz 3

Please take the short Week 3 quiz on Moodle (also due by Tuesday night). This is to help me understand what areas need work, and also help you understand what concepts might need extra practice. It is not a large part of the overall homework grade. Just for the quiz part, do not discuss the questions with other students, TAs, etc. (Note: quiz usually posted Wednesday afternoon.)

Chatbot overview

In this assignment you will be building a chatbot that can interact with a human user. To learn more about early chatbots, you can read about ELIZA, who was developed in the 60s. You can talk to ELIZA online. A related idea is the Turing Test (developed by Alan Turing), where a human interacts with an artificial intelligence through a chat interface, and tries to determine if the program is indistinguishable from interacting with a human.

Your task in this assignment is to first ask the user for the number of rounds of conversation. Then the computer should print a greeting. After that, there will be the specified number of rounds of conversation (user going first, computer going second). Finally, the computer should print a greeting and the conversation is over. Here is a sample output:

conversation1

The following steps will help you with the specific details and program requirements. As you're developing your code, make sure to use comments and informative variables names.

Step by step

  1. First, create a new Python file called hw3.py. It might be a good idea to start from the program echo.py that we developed in class. Modify this program so that the user can specify the number of rounds of conversation, similar to the example above. Also add a greeting and a salutation (I have used "Hello! What's on your mind?" and "Goodbye!", but feel free whatever you like).

  2. In the echo.py program, the user was prompted with "Say something: ". But we don't have to have a user prompt. Modify your program to use the empty string as a prompt, so that the user can simply start typing a sentence on a new line.

  3. Consider the first exchange in the example. The "I" and "am" are replaced with "You" and "are". In your program, you will "mirror" certain words, so that the conversation is more realistic (and to practice conditionals!) To implement this part, first split the sentence into a list of words (see notes from Monday). Now create another for-loop inside the first for-loop. So the idea is that the first for-loop will go over each round of conversation, and the second for loop will go through each word of the sentence for that round. To start, use this inner for-loop to print out each user word. At the end of these first three steps, you should have something like this:

    step3

  4. Now we will change some of these words to a "mirrored" word. So within the inner for-loop, create an if-statement that tests if the word is equal to "I". If so, print "You". Then create an elif-statement that tests if the word is equal to "am". If so, print "are". Then in the else case, just print what the word was before. So you should get something that looks like this:

    step4

    Here is a table of all the words you should account for (feel free to add more though):

    User Word Computer Word
    I You
    You I
    am are
    are am
    I'm You're
    You're I'm

    It is difficult to be completely general when it comes to capital/lowercase, so don't worry about this for now unless you want to do it as an extension later on.

  5. However, right now these printed words are all printed separately. To fix this, define a variable outside the inner for-loop to keep track of the computer's response. Then use string concatenation inside the for-loop to gradually build up the computer's response by adding on a word each time. Then outside the for-loop, add a question mark (similar to the echo program). You should get something like this:

    step4

  6. Right now, if the user's sentence does not contain a mirrored word, the sentence will simply be echoed back to them. To make things a bit more interesting, if no mirrored words are found, choose a sentence at random from a pre-specified list. For my list I used:
    
          "Why is the sky blue?"
          "What does it all mean?"
          "What are we here for?"
          "When is lunch?"
          "Tell me about your family."
                         
    but feel free to add/modify this list, as long as you have 5 "canned" responses. Define this list outside your for-loops.

  7. After creating this list, we need some way of being able to tell when we should use it. To do this, create a variable inside the first for-loop, but before starting the second for-loop. This variable will keep track of whether or not a mirrored word was found. For example, you could define the variable mirrored_word_found = False. This means that initially, we haven't found any mirrored words. Then, if any of our if/elif cases are true, we will change mirrored_words_found to True. At the end of the inner for-loop, if we haven't found any mirrored words, we will use a random canned response.

    The figure below shows the first example again, but annotated to show these features:

    conversation2

Requirements

To summarize the steps above, here is a list of requirements for your program:

  • The number of rounds of conversation must be flexible and determined by the user.

  • Computer's response must be stored within a separate variable, and built up gradually using string concatenation.

  • All mirrored words must be replaced (using at least the list above, but feel free to add more).

  • If any mirrored words are found, add a question mark to the end of the sentence.

  • If there are no mirrored words in the sentence, choose a response at random from a list of "canned" responses.

  • You should include a greeting and a salutation.

  • Comments (including a header) and good variable names must be used.

Extensions

There are several ways to extend your chatbot to be more realistic and/or creative. The suggestions below are not for credit or extra credit, but are a great opportunity for further practice. Feel free to diverge from this list and create your own. I will demo a few chatbots in class.

  • Right now the question mark is a space away from the last word. Fix this.

  • Right now capital/lowercase letters are not taken into account when mirroring words. Fix this.

  • To make the conversation more flexible, you can modify your program to pickup on "key words". Create a list of key words (i.e. happy, sad, mother, father, swimming, volleyball, etc). Then if the user mentions any of these words, ask a question about it. For example:
    
          I am going swimming               # user
          Tell me more about your swimming  # computer
          It all comes down to my mother    # user
          Tell me more about your mother    # computer
        

Transcript and Submit

Finally, create a transcript that demonstrates your code working for at least 2 conversations of different length. Save this file as hw3_transcript.txt. Also make sure your code is commented, and that you have given some thought to the header, variable names, and whitespace.

TO SUBMIT ON MOODLE:

  • hw3.py

  • hw3_transcript.txt