Computer Science 111. Fall, 2006
Homework #4
due: by midnight on October 4
Note: Make sure the inputs required of the user are exactly as specified and in the right order.
Your task in this week's homework is to
make a better version of the interactive conversation program.
Part 1.
Get a copy of the file hw4Judy.pyc from my handout directory (with the command getcopy hw4Judy.pyc).
import hw4Judy and then hw4Judy.main().
Use emacs to write your own version of the program. Here is what your program should do:
Set up cannedlist to
hold a sequence of at least 8 strings that you make up.
- Compute the number of strings in cannedlist and place that
value in another variable (e.g. I called it num_canned).
Do not assign the variable the number directly, such as 8.
Compute the number some way. That way if you add a canned
response, you don't have to change the number farther down in your code.
This is one aspect of modular programming.
- Now ask the user how many times to interact and place the answer
in a variable.
- Get a random canned response from the sequence of canned responses.
Use your knowledge of randrange from lab4. Place this string in
a variable called computer.
- Print the string stored in variable
computer and get a response from the user. This will be a string and the
user should not have to type quotes around it. Put the
response in a variable called user. This is the first interaction.
- append both strings to a list stored in a variable called
conversation. Recall that we appended sound files to a list in Lab/Hw3.
Make sure to initialize conversation to be the empty list.
- Now use a counted loop for the remaining interactions.
I will call this the outer loop.
The body
of the outer loop will make one computer response, either constructed or
canned, and then get the next user response. Here's how it will work:
- After the if-else statement in the outer loop,
make sure to append the computer and user strings to the
list stored in conversation.
- After that, print out the computer response, and get the next user string.
- The last statement in the body of the outer for loop should be
to re-initialize the variable num_occurrences to 0.
- After the body of the outer for loop, print a goodbye message
to the screen and also append the same message out to the
list stored in conversation.
- Finally, print a few blank lines, the print the conversation,
using the list stored in conversation. Make sure there
is one string printed per line, and no blank lines in between.
Notes and Reminders
Don't worry about punctuation marks.
The total number of inputs from the user should be
1 for the number of interactions, plus
exactly the same number of strings from the user as
is indicated in num_interactions.
In other words the total number of inputs from the
user is num_interactions+1. The first is an integer
and the rest are strings. If you are unsure,
count them as you run your program and type in the
strings.
Remember, all of the interaction must be appended to the
list stored in conversation. This means
that each string the user types in must be appended, and
the computer's response appended.
Your final program should be in a file called hw4.py Submit this homework by the due date with the command submit homework4 hw4.py