L548: Final Exam

Notes:
a) Send the exam to upriss@indiana.edu by Monday, April 29, 12.00 pm (Noon).
Send 2 scripts as attachments to an email message. The 2 files should be labeled yourname1, yourname2. All files must be normal, uncompressed text (no MS Word documents!!!).
b) You will get a better grade if you produce programs that run without errors but do not have all required features than if you work on all the features but your programs cannot be executed.
c) The scripts must be completely written in Python. System calls to Unix utilities or other programming languages are not allowed.
d) No libraries other than the ones that were covered in class, such as import re, should be used.
e) Team work is not allowed. If you have questions, send them to upriss@indiana.edu. If the question can be answered without giving too much help, the answer will be posted to the class mailing list.
f) If you use any code from other sources than the class materials or the Core Python Programming book, you must quote your sources.

1) A word game

Write a script that reads the content of a file called "input.txt". This file contains three lines with three sentences, such as

The dog chases the cat to the kitchen.
John gave an apple to Mary.
A student reads Shakespeare in the bathroom.

Each sentence consists of a noun phrase ("The" or "A" or no article + one noun), a verb, another noun phrase ("the" or "a" or no article + one noun), a prepositional phrase and a period (".").

Your script breaks the three sentences into four parts (noun phrase, verb, noun phrase, prepositional phrase) and then randomly assembles one sentence. Examples of such a sentence are ("John chases the cat to the kitchen.", "The dog reads apple to the kitchen.")

Your script prints one randomly assembled sentence at a time to the screen (in one line) then asks the user whether he/she wants to see another sentence. If the answer is "yes" the script continues, if "no", it stops.

Note: Use the random module: "import random". A random number between 0 and 2 can be obtained by the following statement: "random.randrange(0,3,1)". The first number (0) represents the start of the range; the second number (3) is one larger than the end of the range; the third number (1) is always 1 when dealing with integers.

2) An Eliza tool

Write a script that simulates an Eliza bot as follows:

The computer first prints a random greeting message out of 3 possible greeting messages. The user replies. Then the computer prints a random how-are-you message out of three possible messages. The user replies.

Then a loop starts which only finishes if the user types "bye".

In this loop the computer checks whether the last character of the user's input was a question mark or not. In case of a question mark, the computer types an answer such as "I don't know.", "That is a stupid question.", "I will not answer that question.", etc. Then the computer asks a question that starts a new topic, such as "How is your family?", "Where are you right now?", etc.

In case the last character of the user's input was not a question mark, the computer prints a meaningless comment, such as "Sure.", "Cool!", "Very interesting." and then asks the user to restate his/her comment, such as "Why are you saying that?", "Can you explain that?", "Why do you think that?", etc.

In each case the answer and question should be separately and randomly selected from 5 choices.

For usability issues: you should print some kind of prompt (eg "... ") when you expect user input.