Difference between revisions of "CSC111 Lab 8 2018"
(Created page with "~~~~ ---- <bluebox> This lab continues on with the lab from last week. You will need to add obstacles in the graphic window, and make your circle bounce on obstacles, or get...") |
|||
(15 intermediate revisions by the same user not shown) | |||
Line 1: | Line 1: | ||
[[User:Thiebaut|D. Thiebaut]] ([[User talk:Thiebaut|talk]]) 14:42, 25 March 2018 (EDT) | [[User:Thiebaut|D. Thiebaut]] ([[User talk:Thiebaut|talk]]) 14:42, 25 March 2018 (EDT) | ||
---- | ---- | ||
− | + | <onlydft> | |
<bluebox> | <bluebox> | ||
This lab continues on with the lab from last week. You will need to add obstacles in the graphic window, and make your circle bounce on obstacles, or get stuck to obstacles. | This lab continues on with the lab from last week. You will need to add obstacles in the graphic window, and make your circle bounce on obstacles, or get stuck to obstacles. | ||
</bluebox> | </bluebox> | ||
− | + | <br /> | |
+ | __TOC__ | ||
+ | <br /> | ||
=Adding an Obstacle= | =Adding an Obstacle= | ||
<br /> | <br /> | ||
Line 32: | Line 34: | ||
|- | |- | ||
| | | | ||
− | ==Challenge | + | ==Challenge 1== |
|} | |} | ||
[[Image:QuestionMark3.jpg|right|120px]] | [[Image:QuestionMark3.jpg|right|120px]] | ||
Line 49: | Line 51: | ||
| | | | ||
− | ==Challenge | + | ==Challenge 2== |
|} | |} | ||
[[Image:QuestionMark4.jpg|right|120px]] | [[Image:QuestionMark4.jpg|right|120px]] | ||
Line 103: | Line 105: | ||
|- | |- | ||
| | | | ||
− | ==Challenge | + | ==Challenge 3== |
|} | |} | ||
[[Image:QuestionMark5.jpg|right|120px]] | [[Image:QuestionMark5.jpg|right|120px]] | ||
Line 126: | Line 128: | ||
<br /> | <br /> | ||
<br /> | <br /> | ||
+ | <!-- | ||
=Moodle Submission= | =Moodle Submission= | ||
<br /> | <br /> | ||
Line 132: | Line 135: | ||
:* Information on how to capture a portion of the screen on Windows can be found [http://windows.microsoft.com/en-us/windows/take-screen-capture-print-screen#take-screen-capture-print-screen=windows-8 here]. | :* Information on how to capture a portion of the screen on Windows can be found [http://windows.microsoft.com/en-us/windows/take-screen-capture-print-screen#take-screen-capture-print-screen=windows-8 here]. | ||
<br /> | <br /> | ||
+ | --> | ||
+ | <!-- ================================================================ --> | ||
− | |||
=Using Boolean Functions= | =Using Boolean Functions= | ||
<br /> | <br /> | ||
Line 206: | Line 210: | ||
<br /> | <br /> | ||
<br /> | <br /> | ||
− | Demonstrate the correct operations of your programs to your Lab Instructor | + | Demonstrate the correct operations of your programs to your Lab Instructor. |
<br /> | <br /> | ||
<br /> | <br /> | ||
+ | |||
=getColor() Function= | =getColor() Function= | ||
<br /> | <br /> | ||
− | * | + | * Your assignment is to create a new function called '''getColor()'''. This function should receive the x and y coordinate of the center of the ball, along with the width and height of the window. It will return a string which is the name of a color. If the x and y coordinates passed to the function are inside the top left quarter of the window, the function will return the string 'yellow'. If the x and y coordinates are inside the top right quarter, the function will return the string 'red'. For the lower-left quarter, the function will return 'blue'. And finally, the function will return 'brown' for the bottom-right quarter. |
* Here is an example of how the function can be used '''inside''' the while loop: | * Here is an example of how the function can be used '''inside''' the while loop: | ||
Line 224: | Line 229: | ||
circ.setFill( color ) | circ.setFill( color ) | ||
</source> | </source> | ||
− | ================================================================= --> | + | <!-- ================================================================= --> |
+ | |||
+ | =Eliza= | ||
+ | <br /> | ||
+ | |||
+ | <bluebox> | ||
+ | Today we are going to do some therapy with Eliza. | ||
+ | </bluebox> | ||
+ | <br /> | ||
+ | |||
+ | |||
+ | Eliza is named after Eliza Doolittle, of the 1913 George Bernard Shaw play ''Pigmalion'', which is the inspiration for the musical ''My Fair Lady'', of which you can find many snipets on YouTube, as illusrated below. | ||
+ | <br /> | ||
+ | <center><videoflash>MJr9SSJKkII</videoflash></center> | ||
+ | <br /> | ||
+ | |||
+ | Joseph Weizenbaum originated the program. A good description of what how Eliza works can be found [http://en.wikipedia.org/wiki/ELIZA here]. | ||
+ | |||
+ | =An initial Eliza program= | ||
+ | |||
+ | Copy and paste the program below into a file called '''eliza.py'''. Feel free to work with Idle or with emacs. | ||
+ | <br /> | ||
+ | <source lang="python"> | ||
+ | # eliza.py | ||
+ | # | ||
+ | # a very simple beginning for an eliza-like program | ||
+ | # | ||
+ | # 1) greet user | ||
+ | # 2) repeat a huge number of times: | ||
+ | # 2.1) get user's statement about life | ||
+ | # 2.2) respond in a way that seems intelligent | ||
+ | # | ||
+ | # Addition: | ||
+ | # cycles through a series of canned answers to get | ||
+ | # the user to think the computer is listening | ||
+ | import random | ||
+ | |||
+ | canned = [ "\nPlease tell me more", | ||
+ | "\nI see...", | ||
+ | "\nI am listening..." ] | ||
+ | |||
+ | def main(): | ||
+ | |||
+ | # greet user | ||
+ | print( "Welcome. Please tell me of your problems: " ) | ||
+ | print( '(You may quit at any time by answering "bye")' ) | ||
+ | |||
+ | # repeat a huge number of times | ||
+ | for i in range( 10000 ): | ||
+ | |||
+ | # get user's statement about her life | ||
+ | answer = input( "\n> " ) | ||
+ | if ( answer.lower() == "bye" ): | ||
+ | break | ||
+ | |||
+ | # respond semi-intelligently | ||
+ | # cycle through the list of canned sentences... | ||
+ | print( random.choice( canned ) ) | ||
+ | |||
+ | main() | ||
+ | |||
+ | </source> | ||
+ | <br /> | ||
+ | <br /> | ||
+ | |||
+ | |||
+ | * Run the program and enter at least 6 or 7 sentences. Observe how the program repeats itself in the way it responds to the user. | ||
+ | |||
+ | * Go ahead and add a few more canned answers if you wish. This will make it harder for a human to figure out the repetition, but not foolproof. | ||
+ | |||
+ | * By the way, you do not have to type sentences that make sense to test your program. Just the letter 'a' is enough of an input! | ||
+ | |||
+ | =Detect Family Members= | ||
+ | |||
+ | * Let's start by checking to see if the user is mentioning "mother" in the conversation. If so, we'll have our program respond with some family-oriented statement. | ||
+ | |||
+ | * How can we see if the user mentioned ''mother'' in her sentence? There are several possibilities. The one we'll use here is to use the string ''find()'' method. To see how it works, try these python statements in the python shell: | ||
+ | |||
+ | |||
+ | >>> answer = "My mother sent me" | ||
+ | >>> answer.find( "mother" ) | ||
+ | ... | ||
+ | >>> answer.find( "My" ) | ||
+ | ... | ||
+ | >>> answer.find( "father" ) | ||
+ | ... | ||
+ | >>> answer.find( "me" ) | ||
+ | ... | ||
+ | >>> answer.find( "MOTHER" ) | ||
+ | ... | ||
+ | >>> | ||
+ | |||
+ | * Notice that ''find()'' returns the ''index'' of where a word appears in the string '''answer'''. If the string doesn't appear in ''answer'', then ''find()'' returns -1. | ||
+ | |||
+ | * So, something like this should work for us: | ||
+ | <br /> | ||
+ | <br /> | ||
+ | <source lang="python"> | ||
+ | answer = input( "> " ) | ||
+ | |||
+ | # did user mention "mother"? | ||
+ | if answer.find( "mother" ) != -1: | ||
+ | print( "tell me more about your family, please." ) | ||
+ | |||
+ | </source> | ||
+ | <br /> | ||
+ | <br /> | ||
+ | {| style="width:100%; background:silver" | ||
+ | |- | ||
+ | | | ||
+ | ==Challenge 1== | ||
+ | |} | ||
+ | [[Image:QuestionMark1.jpg|right|120px]] | ||
+ | |||
+ | * Integrate this solution in your program. Make sure your program detects correctly the word ''mother'' whether it is spelled in lower or upper case. | ||
+ | <br /> | ||
+ | <br /> | ||
+ | <br /> | ||
+ | <br /> | ||
+ | <br /> | ||
+ | {| style="width:100%; background:silver" | ||
+ | |- | ||
+ | | | ||
+ | ==Challenge 2== | ||
+ | |} | ||
+ | [[Image:QuestionMark4.jpg|right|120px]] | ||
+ | |||
+ | * Make your program check as well for the words ''father'', ''brother'', ''sister'', ''son'', ''daughter''. | ||
+ | <br /> | ||
+ | <br /> | ||
+ | <br /> | ||
+ | <br /> | ||
+ | |||
+ | =Simple Reflection= | ||
+ | |||
+ | * One way for Eliza to appear intelligent is to say the same thing the user just said, but to change the sentence from the first person to the second person. For example, if the user says "I am tired", Eliza might respond "You are tired?". | ||
+ | |||
+ | * We can program that by replacing all occurrences of "I" by "you", "am" by "are", "my" by "your", and "me" by "you". | ||
+ | |||
+ | * Here is a possible solution (code it in a separate program): | ||
+ | |||
+ | <br /> | ||
+ | <source lang="python"> | ||
+ | def main(): | ||
+ | answer = input( "\n> " ) | ||
+ | |||
+ | # split user's sentence into words | ||
+ | words = answer.split( ) | ||
+ | |||
+ | #create a copy new list of words that will be Eliza's response | ||
+ | newWords = [] | ||
+ | for word in words: | ||
+ | newWords.append( word ) | ||
+ | |||
+ | # take each word of user sentence and if the word represents | ||
+ | # the first person singular, switch it to second person. | ||
+ | for i in range( len( words ) ): | ||
+ | if words[i] == "I": | ||
+ | newWords[i] = "you" | ||
+ | |||
+ | # join the list of words in newWords in a string, and print it | ||
+ | sentence = ' '.join( newWords ) | ||
+ | print( sentence ) | ||
+ | |||
+ | main() | ||
+ | </source> | ||
+ | <br /> | ||
+ | |||
+ | * Go ahead and implement this modification. Play with it. Verify that when you use "I" in a sentence, the program replaces it by "you". Understand why the program actually does that. | ||
+ | <br /> | ||
+ | <br /> | ||
+ | {| style="width:100%; background:silver" | ||
+ | |- | ||
+ | | | ||
+ | ==Challenge 3== | ||
+ | |} | ||
+ | [[Image:QuestionMark2.jpg|right|120px]] | ||
+ | |||
+ | |||
+ | * Once you have tested this, go ahead and make your program also replace | ||
+ | ** "me" by "you" | ||
+ | ** "am" by "are" | ||
+ | ** "I've" by "you've" | ||
+ | ** "I'm" by "you're" | ||
+ | ** "I'll" by "you'll" | ||
+ | ** "was" by "were" | ||
+ | ** "my" by "your" | ||
+ | ** "mine" by "yours" | ||
+ | |||
+ | <br /> | ||
+ | <br /> | ||
+ | |||
+ | ==Adding a ? at the end of the reflected sentence== | ||
+ | |||
+ | * Once you have your list of new words newWords containing the reflected words, either modify the last word of this list and add a "?" at the end of it, or simply add a "?" to the end of the sentence. | ||
+ | |||
+ | ==Reflection or Canned Answer?== | ||
+ | |||
+ | * Depending on how you have modified your program, it may respond to the user with two sentences. One reflected and one canned. | ||
+ | |||
+ | * What you should do is make your program output the reflected answer when it finds out that it was able to change some of the words in the user's string, and make it output a canned answer when there is no reflection. | ||
+ | |||
+ | * For example, if the user says "I am bored", your Eliza program will respond "you are bored?", but if the user says "never!", your program will respond with an answer of the type "Please go on..." | ||
+ | |||
+ | |||
+ | '' '''Hint #1''': you could compare the original list of words entered by the user, to the list of words generated after the reflection.'' | ||
+ | |||
+ | '' '''Hint #2''': Remember that we saw the '''continue''' statement, and how it works...'' | ||
+ | |||
+ | <br /> | ||
+ | <br /> | ||
+ | {| style="width:100%; background:silver" | ||
+ | |- | ||
+ | | | ||
+ | ==Challenge 4== | ||
+ | |} | ||
+ | [[Image:QuestionMark3.jpg|right|120px]] | ||
+ | |||
+ | |||
+ | Test your program! It should be able to reflect sentences such as "I like you", and should detect when feed it a family-related word. And if it can't reflect and doesn't find a reference to a family member, it should respond with a canned answer. It should really start showing some amount of (fake) artificial intelligence! | ||
+ | |||
+ | <br /> | ||
+ | <br /> | ||
+ | <br /> | ||
+ | |||
+ | =Second level reflection: Switching second person to first person= | ||
+ | |||
+ | * What if the user says to the program "I don't like you"? | ||
+ | |||
+ | * Right now your program will output "you don't like you?" which is not quite right. So we should try to fix this. | ||
+ | |||
+ | <br /> | ||
+ | <br /> | ||
+ | <br /> | ||
+ | <br /> | ||
+ | {| style="width:100%; background:silver" | ||
+ | |- | ||
+ | | | ||
+ | ==Challenge 5== | ||
+ | |} | ||
+ | [[Image:QuestionMark5.jpg|right|120px]] | ||
+ | <br /> | ||
+ | * Go ahead, and make your program replace the 2nd person in the user's input by the first person. You will see that without doing a semantic analysis of the sentence, it is impossible to know whether "you" should be replaced by "I", or by "me". At this point, we will not worry about this, and simply replace "you" by "me" and this will have a higher probability of looking correct. | ||
+ | |||
+ | * If your modification is correct, and the user enters "I don't like you", your program should output "you don't like me?". | ||
+ | <br /> | ||
+ | =Catching key words= | ||
+ | |||
+ | * You may have noticed that with the real Eliza program, when your answer is a single word such as "No", or "never", the program picks up on this and responds something like "why are you so negative?". | ||
+ | |||
+ | * Let's make your program pick up on a single-word "NO" answer. | ||
+ | |||
+ | * All you need is an if statement that will test whether the user's answer is one word only, and this word is "no". | ||
+ | |||
+ | <br /> | ||
+ | <source lang="python"> | ||
+ | answer = input( "> " ) | ||
+ | if answer.lower() =="no": | ||
+ | print( "you are begin very negative today!" ) | ||
+ | ... | ||
+ | |||
+ | </source> | ||
+ | <br /> | ||
+ | |||
+ | |||
+ | * Integrate this new test in your program. | ||
+ | |||
+ | ==Random "negative canned" answers== | ||
+ | |||
+ | Instead of your program always outputting "you are begin very negative today!" in the case explored in the previous section, make it select its response from a new series of canned negative responses (like "No?", "Really?", "You are awfully negative", "why not?", etc.) | ||
+ | |||
+ | <br /> | ||
+ | <br /> | ||
+ | {| style="width:100%; background:silver" | ||
+ | |- | ||
+ | | | ||
+ | ==Challenge 6== | ||
+ | |} | ||
+ | [[Image:QuestionMark8.jpg|right|120px]] | ||
+ | |||
+ | <br /> | ||
+ | * What if we not only wanted our program to pick up on "No", but also on "Never", "Niet", or "Not"? All we need to do is change the if statement to read "if the user's answer contains 1 word only, and this word is "no" or "never"..." | ||
+ | |||
+ | * Here is the test in python: | ||
+ | |||
+ | <br /> | ||
+ | <source lang="python"> | ||
+ | if answer in [ "no", "never", "niet", "not" ]: | ||
+ | print( random.choice( cannedNegative ) ) | ||
+ | </source> | ||
+ | <br /> | ||
+ | |||
+ | <br /> | ||
+ | <br /> | ||
+ | {| style="width:100%; background:silver" | ||
+ | |- | ||
+ | | | ||
+ | ==Challenge of the Day== | ||
+ | |} | ||
+ | [[Image:QuestionMark9.jpg|right|120px]] | ||
+ | |||
+ | <br /> | ||
+ | How can we make Eliza remember some of the sentences she said before, and make her repeat them later? For example, assume that at some point Eliza tells the user "You like me?" as a reflection of "I like you". Then, a few minutes later, when Eliza doesn't find anything interesting in what the user says (no negation, no family members, nothing to reflect), instead of saying a simple canned answer, Eliza could say "Is that why you like me?". In other words, make the reflected answers from Eliza also become part of the canned answers, with "Is that" appended in front of them... | ||
+ | <br /> | ||
+ | |||
+ | <br /> | ||
+ | =Moodle Submission= | ||
+ | <br /> | ||
+ | Submit your most sophisticated program to Moodle, in the Lab8 section. You won't be able to evaluate it. It will be tested by hand later on. | ||
+ | <br /> | ||
+ | Make sure your program is well documented, contains a header with your name(s), and a description of what it does. Your level of organization and documentation will be graded, along with an evaluation of how well your program responds to user input. | ||
+ | |||
+ | <br /> | ||
+ | |||
+ | =Learning More about Eliza= | ||
+ | |||
+ | * If you want to know more about this important program in the history of computer science, you may enjoy reading [http://www.cs.umbc.edu/courses/331/papers/eliza.html this paper]. | ||
+ | |||
+ | <br /> | ||
+ | </onlydft> | ||
+ | <showafterdate after="20180402 00:00" before="20180601 00:00"> | ||
+ | <!-- ======================================================================== --> | ||
+ | =Solution Programs= | ||
+ | |||
+ | <br /> | ||
+ | ==demo5.py== | ||
+ | <br /> | ||
+ | <source lang="python"> | ||
+ | # Uses graphics.py from Zelle | ||
+ | # | ||
+ | # This program displays the basic elements of a program | ||
+ | # | ||
+ | from graphics import * | ||
+ | |||
+ | def main(): | ||
+ | win = GraphWin( "Lab 7 Moving ball", 600, 400 ) | ||
+ | |||
+ | # create a green obstacle in the middle of the window | ||
+ | x1 = 200 | ||
+ | y1 = 200 | ||
+ | x2 = 250 | ||
+ | y2 = 250 | ||
+ | obstacle1 = Rectangle( Point( x1, y1 ), Point( x2, y2 ) ) | ||
+ | obstacle1.setFill( "green" ) | ||
+ | obstacle1.draw( win ) | ||
+ | |||
+ | # create another green rectangle on the right of the first one | ||
+ | x3 = 350 | ||
+ | y3 = 200 | ||
+ | x4 = 400 | ||
+ | y4 = 250 | ||
+ | obstacle2 = Rectangle( Point( x3, y3 ), Point( x4, y4 ) ) | ||
+ | obstacle2.setFill( "magenta" ) | ||
+ | obstacle2.draw( win ) | ||
+ | |||
+ | # create and draw a red circle | ||
+ | radius = 30 | ||
+ | center = Point( 100, 100 ) | ||
+ | circ = Circle( center, radius ) | ||
+ | circ.setFill( 'red' ) | ||
+ | circ.draw( win ) | ||
+ | |||
+ | # define the direction the circle will start moving in. | ||
+ | # 5 pixels to the right every move, and 0.25 pixels down | ||
+ | # every move. | ||
+ | dx = 5.1 | ||
+ | dy = 2.51 | ||
+ | |||
+ | # as long as the mouse hasn't been clicked on the window | ||
+ | # keep on looping. | ||
+ | while win.checkMouse() == None: | ||
+ | |||
+ | # move the circle in the current direction. | ||
+ | circ.move( dx, dy ) | ||
+ | |||
+ | # get the x and y of the center of the circle. | ||
+ | x = circ.getCenter().getX() | ||
+ | y = circ.getCenter().getY() | ||
+ | |||
+ | # if the center of the ball is outside the right or left boundary, | ||
+ | # reverse the x direction of movement. | ||
+ | if x > 600 - radius or x < radius: | ||
+ | dx = -dx | ||
+ | if y > 400 - radius or y < radius: | ||
+ | dy = -dy | ||
+ | |||
+ | # if the center of the ball is inside the first obstacle, stop | ||
+ | # the ball. | ||
+ | if x1 < x < x2 and y1 < y < y2 : | ||
+ | dx = 0 | ||
+ | dy = 0 | ||
+ | |||
+ | # if the center of the ball is inside the second obstacle, bounce | ||
+ | # off | ||
+ | if x3 < x < x4 and y3 < y < y4 : | ||
+ | dx = -dx | ||
+ | dy = -dy | ||
+ | |||
+ | |||
+ | # if we're here, it's because the the user clicked on the graphic window. | ||
+ | # we can close everything and quit. | ||
+ | win.close() | ||
+ | |||
+ | main() | ||
+ | |||
+ | |||
+ | |||
+ | |||
+ | </source> | ||
+ | <br /> | ||
+ | ==demo6.py== | ||
+ | <br /> | ||
+ | <source lang="python"> | ||
+ | # Uses graphics.py from Zelle | ||
+ | # | ||
+ | # This program displays the basic elements of a program | ||
+ | # | ||
+ | from graphics import * | ||
+ | |||
+ | # ifInside: returns true of the coordinates of a point defined by | ||
+ | # its coordinates ballX and ballY, are inside the rectangle defined by a top | ||
+ | # left point of coordinates obsX1, obsY1, and a bottom-right point | ||
+ | # with coordinates obsX2, obsY2. Returns false if the point is outside | ||
+ | # the rectangle. | ||
+ | def isInside( ballX, ballY, obsX1, obsY1, obsX2, obsY2 ): | ||
+ | if obsX1 < ballX < obsX2 and obsY1 < ballY < obsY2: | ||
+ | return True | ||
+ | else: | ||
+ | return False | ||
+ | |||
+ | |||
+ | def main(): | ||
+ | win = GraphWin( "Lab 7 Moving ball", 600, 400 ) | ||
+ | |||
+ | # create a green obstacle in the middle of the window | ||
+ | x1 = 200 | ||
+ | y1 = 200 | ||
+ | x2 = 250 | ||
+ | y2 = 250 | ||
+ | obstacle1 = Rectangle( Point( x1, y1 ), Point( x2, y2 ) ) | ||
+ | obstacle1.setFill( "green" ) | ||
+ | obstacle1.draw( win ) | ||
+ | |||
+ | # create another green rectangle on the right of the first one | ||
+ | x3 = 350 | ||
+ | y3 = 200 | ||
+ | x4 = 400 | ||
+ | y4 = 250 | ||
+ | obstacle2 = Rectangle( Point( x3, y3 ), Point( x4, y4 ) ) | ||
+ | obstacle2.setFill( "magenta" ) | ||
+ | obstacle2.draw( win ) | ||
+ | |||
+ | # create and draw a red circle | ||
+ | radius = 30 | ||
+ | center = Point( 100, 100 ) | ||
+ | circ = Circle( center, radius ) | ||
+ | circ.setFill( 'red' ) | ||
+ | circ.draw( win ) | ||
+ | |||
+ | # define the direction the circle will start moving in. | ||
+ | # 5 pixels to the right every move, and 0.25 pixels down | ||
+ | # every move. | ||
+ | dx = 5.111 | ||
+ | dy = -2.51 | ||
+ | |||
+ | # as long as the mouse hasn't been clicked on the window | ||
+ | # keep on looping. | ||
+ | while win.checkMouse() == None: | ||
+ | |||
+ | # move the circle in the current direction. | ||
+ | circ.move( dx, dy ) | ||
+ | |||
+ | # get the x and y of the center of the circle. | ||
+ | x = circ.getCenter().getX() | ||
+ | y = circ.getCenter().getY() | ||
+ | |||
+ | # if the center of the ball is outside the right or left boundary, | ||
+ | # reverse the x direction of movement. | ||
+ | if x > 600 - radius or x < radius: | ||
+ | dx = -dx | ||
+ | if y > 400 - radius or y < radius: | ||
+ | dy = -dy | ||
+ | |||
+ | # if the center of the ball is inside the first obstacle, stop | ||
+ | # the ball. | ||
+ | if isInside( x, y, x1, y1, x2, y2 ): | ||
+ | dx = 0 | ||
+ | dy = 0 | ||
+ | |||
+ | # if the center of the ball is inside the second obstacle, bounce | ||
+ | # off | ||
+ | if isInside( x, y, x3, y3, x4, y4 ): | ||
+ | dx = -dx | ||
+ | dy = -dy | ||
+ | |||
+ | |||
+ | # if we're here, it's because the the user clicked on the graphic window. | ||
+ | # we can close everything and quit. | ||
+ | win.close() | ||
+ | |||
+ | main() | ||
+ | |||
+ | |||
+ | |||
+ | |||
+ | </source> | ||
+ | <br /> | ||
+ | ==demo7.py== | ||
+ | <br /> | ||
+ | <source lang="python"> | ||
+ | # Uses graphics.py from Zelle | ||
+ | # | ||
+ | # This program displays the basic elements of a program | ||
+ | # | ||
+ | from graphics import * | ||
+ | |||
+ | # ifInside: returns true of the coordinates of a point defined by | ||
+ | # its coordinates ballX and ballY, are inside the rectangle defined by a top | ||
+ | # left point of coordinates obsX1, obsY1, and a bottom-right point | ||
+ | # with coordinates obsX2, obsY2. Returns false if the point is outside | ||
+ | # the rectangle. | ||
+ | def isInside( ballX, ballY, obsX1, obsY1, obsX2, obsY2 ): | ||
+ | if obsX1 < ballX < obsX2 and obsY1 < ballY < obsY2: | ||
+ | return True | ||
+ | else: | ||
+ | return False | ||
+ | |||
+ | def isLeftSide( x, y, width ): | ||
+ | if x < width//2: | ||
+ | return True | ||
+ | else: | ||
+ | return False | ||
+ | |||
+ | def main(): | ||
+ | win = GraphWin( "Lab 7 Moving ball", 600, 400 ) | ||
+ | |||
+ | # create a green obstacle in the middle of the window | ||
+ | x1 = 200 | ||
+ | y1 = 200 | ||
+ | x2 = 250 | ||
+ | y2 = 250 | ||
+ | obstacle1 = Rectangle( Point( x1, y1 ), Point( x2, y2 ) ) | ||
+ | obstacle1.setFill( "green" ) | ||
+ | obstacle1.draw( win ) | ||
+ | |||
+ | # create another green rectangle on the right of the first one | ||
+ | x3 = 350 | ||
+ | y3 = 200 | ||
+ | x4 = 400 | ||
+ | y4 = 250 | ||
+ | obstacle2 = Rectangle( Point( x3, y3 ), Point( x4, y4 ) ) | ||
+ | obstacle2.setFill( "magenta" ) | ||
+ | obstacle2.draw( win ) | ||
+ | |||
+ | # create and draw a red circle | ||
+ | radius = 30 | ||
+ | center = Point( 100, 100 ) | ||
+ | circ = Circle( center, radius ) | ||
+ | circ.setFill( 'red' ) | ||
+ | circ.draw( win ) | ||
+ | |||
+ | # define the direction the circle will start moving in. | ||
+ | # 5 pixels to the right every move, and 0.25 pixels down | ||
+ | # every move. | ||
+ | dx = 5.111 | ||
+ | dy = -2.51 | ||
+ | |||
+ | # as long as the mouse hasn't been clicked on the window | ||
+ | # keep on looping. | ||
+ | while win.checkMouse() == None: | ||
+ | |||
+ | # move the circle in the current direction. | ||
+ | circ.move( dx, dy ) | ||
+ | |||
+ | # get the x and y of the center of the circle. | ||
+ | x = circ.getCenter().getX() | ||
+ | y = circ.getCenter().getY() | ||
+ | |||
+ | # if the center of the ball is outside the right or left boundary, | ||
+ | # reverse the x direction of movement. | ||
+ | if x > 600 - radius or x < radius: | ||
+ | dx = -dx | ||
+ | if y > 400 - radius or y < radius: | ||
+ | dy = -dy | ||
+ | |||
+ | # if the center of the ball is inside the first obstacle, stop | ||
+ | # the ball. | ||
+ | if isInside( x, y, x1, y1, x2, y2 ): | ||
+ | dx = 0 | ||
+ | dy = 0 | ||
+ | |||
+ | # if the center of the ball is inside the second obstacle, bounce | ||
+ | # off | ||
+ | if isInside( x, y, x3, y3, x4, y4 ): | ||
+ | dx = -dx | ||
+ | dy = -dy | ||
+ | |||
+ | # if the ball is on the left side of the window, its color is | ||
+ | # red, else its color is yellow. | ||
+ | if isLeftSide( x, y, win.getWidth() )==True: | ||
+ | circ.setFill( 'red' ) | ||
+ | else: | ||
+ | circ.setFill( 'yellow' ) | ||
+ | |||
+ | |||
+ | # if we're here, it's because the the user clicked on the graphic window. | ||
+ | # we can close everything and quit. | ||
+ | win.close() | ||
+ | |||
+ | main() | ||
+ | |||
+ | |||
+ | |||
+ | |||
+ | </source> | ||
+ | <br /> | ||
+ | |||
+ | |||
+ | |||
+ | ==demo8.py== | ||
+ | <br /> | ||
+ | <source lang="python"> | ||
+ | # Uses graphics.py from Zelle | ||
+ | # | ||
+ | # This program displays the basic elements of a program | ||
+ | # | ||
+ | from graphics import * | ||
+ | |||
+ | # ifInside: returns true of the coordinates of a point defined by | ||
+ | # its coordinates ballX and ballY, are inside the rectangle defined by a top | ||
+ | # left point of coordinates obsX1, obsY1, and a bottom-right point | ||
+ | # with coordinates obsX2, obsY2. Returns false if the point is outside | ||
+ | # the rectangle. | ||
+ | def isInside( ballX, ballY, obsX1, obsY1, obsX2, obsY2 ): | ||
+ | if obsX1 < ballX < obsX2 and obsY1 < ballY < obsY2: | ||
+ | return True | ||
+ | else: | ||
+ | return False | ||
+ | |||
+ | def isLeftSide( x, y, width ): | ||
+ | if x < width//2: | ||
+ | return True | ||
+ | else: | ||
+ | return False | ||
+ | |||
+ | def getColor( x, y, w, h ): | ||
+ | if x < w/2 and y < h/2: | ||
+ | return 'yellow' | ||
+ | elif x < w/2 and y > h/2: | ||
+ | return 'blue' | ||
+ | elif x > w/2 and y < h/2: | ||
+ | return 'red' | ||
+ | else: | ||
+ | return 'brown' | ||
+ | |||
+ | def main(): | ||
+ | win = GraphWin( "Lab 7 Moving ball", 600, 400 ) | ||
+ | |||
+ | # create a green obstacle in the middle of the window | ||
+ | x1 = 200 | ||
+ | y1 = 200 | ||
+ | x2 = 250 | ||
+ | y2 = 250 | ||
+ | obstacle1 = Rectangle( Point( x1, y1 ), Point( x2, y2 ) ) | ||
+ | obstacle1.setFill( "green" ) | ||
+ | obstacle1.draw( win ) | ||
+ | |||
+ | # create another green rectangle on the right of the first one | ||
+ | x3 = 350 | ||
+ | y3 = 200 | ||
+ | x4 = 400 | ||
+ | y4 = 250 | ||
+ | obstacle2 = Rectangle( Point( x3, y3 ), Point( x4, y4 ) ) | ||
+ | obstacle2.setFill( "magenta" ) | ||
+ | obstacle2.draw( win ) | ||
+ | |||
+ | # create and draw a red circle | ||
+ | radius = 30 | ||
+ | center = Point( 100, 100 ) | ||
+ | circ = Circle( center, radius ) | ||
+ | circ.setFill( 'red' ) | ||
+ | circ.draw( win ) | ||
+ | |||
+ | # define the direction the circle will start moving in. | ||
+ | # 5 pixels to the right every move, and 0.25 pixels down | ||
+ | # every move. | ||
+ | dx = 5.111 | ||
+ | dy = -2.51 | ||
+ | |||
+ | # as long as the mouse hasn't been clicked on the window | ||
+ | # keep on looping. | ||
+ | while win.checkMouse() == None: | ||
+ | |||
+ | # move the circle in the current direction. | ||
+ | circ.move( dx, dy ) | ||
+ | |||
+ | # get the x and y of the center of the circle. | ||
+ | x = circ.getCenter().getX() | ||
+ | y = circ.getCenter().getY() | ||
+ | |||
+ | circ.setFill( getColor( x, y, win.getWidth(), win.getHeight() ) ) | ||
+ | |||
+ | # if the center of the ball is outside the right or left boundary, | ||
+ | # reverse the x direction of movement. | ||
+ | if x > 600 - radius or x < radius: | ||
+ | dx = -dx | ||
+ | if y > 400 - radius or y < radius: | ||
+ | dy = -dy | ||
+ | |||
+ | # if the center of the ball is inside the first obstacle, stop | ||
+ | # the ball. | ||
+ | if isInside( x, y, x1, y1, x2, y2 ): | ||
+ | dx = 0 | ||
+ | dy = 0 | ||
+ | |||
+ | # if the center of the ball is inside the second obstacle, bounce | ||
+ | # off | ||
+ | if isInside( x, y, x3, y3, x4, y4 ): | ||
+ | dx = -dx | ||
+ | dy = -dy | ||
+ | |||
+ | # if the ball is on the left side of the window, its color is | ||
+ | # red, else its color is yellow. | ||
+ | """ | ||
+ | if isLeftSide( x, y, win.getWidth() )==True: | ||
+ | circ.setFill( 'red' ) | ||
+ | else: | ||
+ | circ.setFill( 'yellow' ) | ||
+ | """ | ||
+ | |||
+ | # if we're here, it's because the the user clicked on the graphic window. | ||
+ | # we can close everything and quit. | ||
+ | win.close() | ||
+ | |||
+ | main() | ||
+ | |||
+ | |||
+ | |||
+ | |||
+ | </source> | ||
+ | <br /> | ||
+ | ==Eliza== | ||
+ | <br /> | ||
+ | ::<source lang="python"> | ||
+ | # eliza.py | ||
+ | # | ||
+ | # This program was developed in class. It is poorly documented, | ||
+ | # and is provided here only to help with the note-taking. | ||
+ | |||
+ | import random | ||
+ | |||
+ | |||
+ | |||
+ | def cannedAnswer(): | ||
+ | """display a random canned answer. | ||
+ | """ | ||
+ | canned = [ "\nPlease tell me more", | ||
+ | "\nI see...", | ||
+ | "\nI am listening..." ] | ||
+ | print( random.choice( canned ) ) | ||
+ | |||
+ | def cannedNegative(): | ||
+ | """display statement picking up on negativity | ||
+ | """ | ||
+ | canned = [ "My, my, so negative today!", | ||
+ | "Really?", "You should be more positive", | ||
+ | "Maybe you should take a break!" ] | ||
+ | print( random.choice( canned ) ) | ||
+ | |||
+ | def cannedFamily( member ): | ||
+ | """display a random statement about the user's family, | ||
+ | or the particular member the user listed in her answer""" | ||
+ | |||
+ | canned = [ "How do you feel about your " + member, | ||
+ | "is that always the case in your family", | ||
+ | "tell me more about your "+member ] | ||
+ | |||
+ | print( random.choice( canned ) ) | ||
+ | |||
+ | def main(): | ||
+ | |||
+ | # greet user | ||
+ | print( "Welcome. Please tell me of your problems: " ) | ||
+ | print( '(You may quit at any time by answering "bye")' ) | ||
+ | |||
+ | # repeat a huge number of times | ||
+ | for i in range( 100000 ): | ||
+ | |||
+ | # get user's statement about her life | ||
+ | answer = input( "\n> " ) | ||
+ | if ( answer.lower() == "bye" ): | ||
+ | break | ||
+ | |||
+ | # negative answer | ||
+ | neg = [ "no", "never" ] | ||
+ | if answer.lower() in neg: | ||
+ | cannedNegative() | ||
+ | continue | ||
+ | |||
+ | # family member | ||
+ | family = ["mother", "father", "sister", "brother", "dog" ] | ||
+ | found = 0 | ||
+ | for member in family: | ||
+ | if answer.find( member ) != -1: | ||
+ | found = 1 | ||
+ | break | ||
+ | if found != 0: | ||
+ | cannedFamily( member ) | ||
+ | continue | ||
+ | |||
+ | # reflection | ||
+ | # split the answer into a list of words | ||
+ | words = answer.split() | ||
+ | |||
+ | # create a new list with the same words | ||
+ | newWords = [] | ||
+ | for word in words: | ||
+ | newWords.append( word ) | ||
+ | |||
+ | # go through all the words in the original list | ||
+ | # and reflect them in the new list. | ||
+ | for i in range( len( words ) ): | ||
+ | if words[i] == "I": | ||
+ | newWords[i] = "you" | ||
+ | if words[i] == "you": | ||
+ | newWords[i] = "me" | ||
+ | |||
+ | # if the two lists of words are different, we must have | ||
+ | # reflected some words... | ||
+ | if words != newWords: | ||
+ | print( ' ' . join( newWords ) + '?') | ||
+ | continue | ||
+ | |||
+ | # respond semi-intelligently if we couldn't detect anything | ||
+ | # relating to negativity, family, or reflection | ||
+ | cannedAnswer() | ||
+ | |||
+ | # we get here when the user quits. | ||
+ | print( "it was nice chatting with you" ) | ||
+ | |||
+ | main() | ||
+ | |||
+ | </source> | ||
+ | |||
+ | </showafterdate> | ||
+ | <br /> | ||
+ | |||
+ | <br /> | ||
+ | |||
+ | <br /> | ||
+ | |||
+ | <br /> | ||
+ | |||
+ | <br /> | ||
+ | |||
+ | <br /> | ||
+ | |||
+ | <br /> | ||
+ | |||
+ | <br /> | ||
+ | |||
+ | <br /> | ||
+ | <br /> |
Latest revision as of 12:58, 1 June 2018
D. Thiebaut (talk) 14:42, 25 March 2018 (EDT)
<showafterdate after="20180402 00:00" before="20180601 00:00">
Solution Programs
demo5.py
# Uses graphics.py from Zelle
#
# This program displays the basic elements of a program
#
from graphics import *
def main():
win = GraphWin( "Lab 7 Moving ball", 600, 400 )
# create a green obstacle in the middle of the window
x1 = 200
y1 = 200
x2 = 250
y2 = 250
obstacle1 = Rectangle( Point( x1, y1 ), Point( x2, y2 ) )
obstacle1.setFill( "green" )
obstacle1.draw( win )
# create another green rectangle on the right of the first one
x3 = 350
y3 = 200
x4 = 400
y4 = 250
obstacle2 = Rectangle( Point( x3, y3 ), Point( x4, y4 ) )
obstacle2.setFill( "magenta" )
obstacle2.draw( win )
# create and draw a red circle
radius = 30
center = Point( 100, 100 )
circ = Circle( center, radius )
circ.setFill( 'red' )
circ.draw( win )
# define the direction the circle will start moving in.
# 5 pixels to the right every move, and 0.25 pixels down
# every move.
dx = 5.1
dy = 2.51
# as long as the mouse hasn't been clicked on the window
# keep on looping.
while win.checkMouse() == None:
# move the circle in the current direction.
circ.move( dx, dy )
# get the x and y of the center of the circle.
x = circ.getCenter().getX()
y = circ.getCenter().getY()
# if the center of the ball is outside the right or left boundary,
# reverse the x direction of movement.
if x > 600 - radius or x < radius:
dx = -dx
if y > 400 - radius or y < radius:
dy = -dy
# if the center of the ball is inside the first obstacle, stop
# the ball.
if x1 < x < x2 and y1 < y < y2 :
dx = 0
dy = 0
# if the center of the ball is inside the second obstacle, bounce
# off
if x3 < x < x4 and y3 < y < y4 :
dx = -dx
dy = -dy
# if we're here, it's because the the user clicked on the graphic window.
# we can close everything and quit.
win.close()
main()
demo6.py
# Uses graphics.py from Zelle
#
# This program displays the basic elements of a program
#
from graphics import *
# ifInside: returns true of the coordinates of a point defined by
# its coordinates ballX and ballY, are inside the rectangle defined by a top
# left point of coordinates obsX1, obsY1, and a bottom-right point
# with coordinates obsX2, obsY2. Returns false if the point is outside
# the rectangle.
def isInside( ballX, ballY, obsX1, obsY1, obsX2, obsY2 ):
if obsX1 < ballX < obsX2 and obsY1 < ballY < obsY2:
return True
else:
return False
def main():
win = GraphWin( "Lab 7 Moving ball", 600, 400 )
# create a green obstacle in the middle of the window
x1 = 200
y1 = 200
x2 = 250
y2 = 250
obstacle1 = Rectangle( Point( x1, y1 ), Point( x2, y2 ) )
obstacle1.setFill( "green" )
obstacle1.draw( win )
# create another green rectangle on the right of the first one
x3 = 350
y3 = 200
x4 = 400
y4 = 250
obstacle2 = Rectangle( Point( x3, y3 ), Point( x4, y4 ) )
obstacle2.setFill( "magenta" )
obstacle2.draw( win )
# create and draw a red circle
radius = 30
center = Point( 100, 100 )
circ = Circle( center, radius )
circ.setFill( 'red' )
circ.draw( win )
# define the direction the circle will start moving in.
# 5 pixels to the right every move, and 0.25 pixels down
# every move.
dx = 5.111
dy = -2.51
# as long as the mouse hasn't been clicked on the window
# keep on looping.
while win.checkMouse() == None:
# move the circle in the current direction.
circ.move( dx, dy )
# get the x and y of the center of the circle.
x = circ.getCenter().getX()
y = circ.getCenter().getY()
# if the center of the ball is outside the right or left boundary,
# reverse the x direction of movement.
if x > 600 - radius or x < radius:
dx = -dx
if y > 400 - radius or y < radius:
dy = -dy
# if the center of the ball is inside the first obstacle, stop
# the ball.
if isInside( x, y, x1, y1, x2, y2 ):
dx = 0
dy = 0
# if the center of the ball is inside the second obstacle, bounce
# off
if isInside( x, y, x3, y3, x4, y4 ):
dx = -dx
dy = -dy
# if we're here, it's because the the user clicked on the graphic window.
# we can close everything and quit.
win.close()
main()
demo7.py
# Uses graphics.py from Zelle
#
# This program displays the basic elements of a program
#
from graphics import *
# ifInside: returns true of the coordinates of a point defined by
# its coordinates ballX and ballY, are inside the rectangle defined by a top
# left point of coordinates obsX1, obsY1, and a bottom-right point
# with coordinates obsX2, obsY2. Returns false if the point is outside
# the rectangle.
def isInside( ballX, ballY, obsX1, obsY1, obsX2, obsY2 ):
if obsX1 < ballX < obsX2 and obsY1 < ballY < obsY2:
return True
else:
return False
def isLeftSide( x, y, width ):
if x < width//2:
return True
else:
return False
def main():
win = GraphWin( "Lab 7 Moving ball", 600, 400 )
# create a green obstacle in the middle of the window
x1 = 200
y1 = 200
x2 = 250
y2 = 250
obstacle1 = Rectangle( Point( x1, y1 ), Point( x2, y2 ) )
obstacle1.setFill( "green" )
obstacle1.draw( win )
# create another green rectangle on the right of the first one
x3 = 350
y3 = 200
x4 = 400
y4 = 250
obstacle2 = Rectangle( Point( x3, y3 ), Point( x4, y4 ) )
obstacle2.setFill( "magenta" )
obstacle2.draw( win )
# create and draw a red circle
radius = 30
center = Point( 100, 100 )
circ = Circle( center, radius )
circ.setFill( 'red' )
circ.draw( win )
# define the direction the circle will start moving in.
# 5 pixels to the right every move, and 0.25 pixels down
# every move.
dx = 5.111
dy = -2.51
# as long as the mouse hasn't been clicked on the window
# keep on looping.
while win.checkMouse() == None:
# move the circle in the current direction.
circ.move( dx, dy )
# get the x and y of the center of the circle.
x = circ.getCenter().getX()
y = circ.getCenter().getY()
# if the center of the ball is outside the right or left boundary,
# reverse the x direction of movement.
if x > 600 - radius or x < radius:
dx = -dx
if y > 400 - radius or y < radius:
dy = -dy
# if the center of the ball is inside the first obstacle, stop
# the ball.
if isInside( x, y, x1, y1, x2, y2 ):
dx = 0
dy = 0
# if the center of the ball is inside the second obstacle, bounce
# off
if isInside( x, y, x3, y3, x4, y4 ):
dx = -dx
dy = -dy
# if the ball is on the left side of the window, its color is
# red, else its color is yellow.
if isLeftSide( x, y, win.getWidth() )==True:
circ.setFill( 'red' )
else:
circ.setFill( 'yellow' )
# if we're here, it's because the the user clicked on the graphic window.
# we can close everything and quit.
win.close()
main()
demo8.py
# Uses graphics.py from Zelle
#
# This program displays the basic elements of a program
#
from graphics import *
# ifInside: returns true of the coordinates of a point defined by
# its coordinates ballX and ballY, are inside the rectangle defined by a top
# left point of coordinates obsX1, obsY1, and a bottom-right point
# with coordinates obsX2, obsY2. Returns false if the point is outside
# the rectangle.
def isInside( ballX, ballY, obsX1, obsY1, obsX2, obsY2 ):
if obsX1 < ballX < obsX2 and obsY1 < ballY < obsY2:
return True
else:
return False
def isLeftSide( x, y, width ):
if x < width//2:
return True
else:
return False
def getColor( x, y, w, h ):
if x < w/2 and y < h/2:
return 'yellow'
elif x < w/2 and y > h/2:
return 'blue'
elif x > w/2 and y < h/2:
return 'red'
else:
return 'brown'
def main():
win = GraphWin( "Lab 7 Moving ball", 600, 400 )
# create a green obstacle in the middle of the window
x1 = 200
y1 = 200
x2 = 250
y2 = 250
obstacle1 = Rectangle( Point( x1, y1 ), Point( x2, y2 ) )
obstacle1.setFill( "green" )
obstacle1.draw( win )
# create another green rectangle on the right of the first one
x3 = 350
y3 = 200
x4 = 400
y4 = 250
obstacle2 = Rectangle( Point( x3, y3 ), Point( x4, y4 ) )
obstacle2.setFill( "magenta" )
obstacle2.draw( win )
# create and draw a red circle
radius = 30
center = Point( 100, 100 )
circ = Circle( center, radius )
circ.setFill( 'red' )
circ.draw( win )
# define the direction the circle will start moving in.
# 5 pixels to the right every move, and 0.25 pixels down
# every move.
dx = 5.111
dy = -2.51
# as long as the mouse hasn't been clicked on the window
# keep on looping.
while win.checkMouse() == None:
# move the circle in the current direction.
circ.move( dx, dy )
# get the x and y of the center of the circle.
x = circ.getCenter().getX()
y = circ.getCenter().getY()
circ.setFill( getColor( x, y, win.getWidth(), win.getHeight() ) )
# if the center of the ball is outside the right or left boundary,
# reverse the x direction of movement.
if x > 600 - radius or x < radius:
dx = -dx
if y > 400 - radius or y < radius:
dy = -dy
# if the center of the ball is inside the first obstacle, stop
# the ball.
if isInside( x, y, x1, y1, x2, y2 ):
dx = 0
dy = 0
# if the center of the ball is inside the second obstacle, bounce
# off
if isInside( x, y, x3, y3, x4, y4 ):
dx = -dx
dy = -dy
# if the ball is on the left side of the window, its color is
# red, else its color is yellow.
"""
if isLeftSide( x, y, win.getWidth() )==True:
circ.setFill( 'red' )
else:
circ.setFill( 'yellow' )
"""
# if we're here, it's because the the user clicked on the graphic window.
# we can close everything and quit.
win.close()
main()
Eliza
# eliza.py # # This program was developed in class. It is poorly documented, # and is provided here only to help with the note-taking. import random def cannedAnswer(): """display a random canned answer. """ canned = [ "\nPlease tell me more", "\nI see...", "\nI am listening..." ] print( random.choice( canned ) ) def cannedNegative(): """display statement picking up on negativity """ canned = [ "My, my, so negative today!", "Really?", "You should be more positive", "Maybe you should take a break!" ] print( random.choice( canned ) ) def cannedFamily( member ): """display a random statement about the user's family, or the particular member the user listed in her answer""" canned = [ "How do you feel about your " + member, "is that always the case in your family", "tell me more about your "+member ] print( random.choice( canned ) ) def main(): # greet user print( "Welcome. Please tell me of your problems: " ) print( '(You may quit at any time by answering "bye")' ) # repeat a huge number of times for i in range( 100000 ): # get user's statement about her life answer = input( "\n> " ) if ( answer.lower() == "bye" ): break # negative answer neg = [ "no", "never" ] if answer.lower() in neg: cannedNegative() continue # family member family = ["mother", "father", "sister", "brother", "dog" ] found = 0 for member in family: if answer.find( member ) != -1: found = 1 break if found != 0: cannedFamily( member ) continue # reflection # split the answer into a list of words words = answer.split() # create a new list with the same words newWords = [] for word in words: newWords.append( word ) # go through all the words in the original list # and reflect them in the new list. for i in range( len( words ) ): if words[i] == "I": newWords[i] = "you" if words[i] == "you": newWords[i] = "me" # if the two lists of words are different, we must have # reflected some words... if words != newWords: print( ' ' . join( newWords ) + '?') continue # respond semi-intelligently if we couldn't detect anything # relating to negativity, family, or reflection cannedAnswer() # we get here when the user quits. print( "it was nice chatting with you" ) main()
</showafterdate>