Logo Lesson 5PrerequisitesObjectivesLogo ListsSlide 5Slide 6Slide 7Sentences in LogoSlide 9Slide 10Slide 11Slide 12Slide 13Slide 14Slide 15Slide 16Slide 17Slide 18Slide 19An Addition DrillSlide 21Slide 22Slide 23Slide 24Slide 25Slide 26An Additional DrillSlide 28Slide 29Slide 30As Addition DrillRecursionSelf Check Lesson 5Slide 34Slide 35Slide 36Slide 37Slide 38Slide 39Slide 40Slide 41Slide 42Slide 43Logo Lesson 5TBE 540-40Fall 2004Farah FisherPrerequisitesGiven a shape, use basic Logo commands and/or a procedure to draw the shape, with and without the use of variables.Use SAVE, LOAD and POTS to manage Logo workspaces.Demonstrate the use of RANDOM.ObjectivesPut a list of words into a variable and print any item of the list.Print a sentence in Logo, with and without variables.Print a sentence in Logo from randomly chosen words.Demonstrate use of IF and IFELSE.Create a Logo procedure that demonstrates the use of recursion.Logo ListsA list in Logo a set of values all stored under the same variable name.Other programming languages call this an array.Each value stored in the list is called an item.Each item has a number, e.g., in the list [HOT COLD DOG], item #3 is DOG.Logo ListsHere is a sample of the creation of a list within a procedure:•MAKE “COLORS [RED GREEN BLUE]If you (print) PR :COLORS, all three colors will appear.If you PR ITEM 2 :COLORS the word GREEN will appear.Logo ListsPR ITEM 1 + RANDOM 3 :COLORS will display a randomly chosen word from the list of three.Lists are very useful in choosing words at random. MAKE “WORD1 ITEM 1 + RANDOM 3 :COLORS would put a randomly chosen word from the list into the variable :WORD1Logo ListsLogo lists may also contain numbers. Here is a list of prime numbers put into a variable called PRIME.•MAKE “PRIME [1 2 3 5 7 11 13 17 19 23]What would appear if you typed this? PR ITEM 6 :PRIME (11 would be printed, since it is the 6th item)How would you choose at random from the list? A possible command:•MAKE “F ITEM 1 + RANDOM 10 :PRIMESentences in LogoYou have seen several examples of the PR (print) command. It can be used to display the value of variables or messages.Next you will see how to put several pieces of information together in a “sentence.”Sentences in LogoIf you already know all the words in a sentence, you can simply put it inside brackets, like PR [HERE IT IS.]But what if you are trying to include variables (words or numbers)?For example, suppose you asked someone to enter his/her name, and you want to print HI and the name he/she entered.Sentences in LogoThe commands to ask for and get the name would be•PR [PLEASE TYPE YOUR NAME.]•MAKE “NM READLISTIf you typed PR [HI :NM] to print, you would not get the result you want. PR [HI :NM] would actually display HI :NM (rather than the actual name), since information in [ ] is taken literally.Sentences in LogoTake a look at the third line of commands:•PR [PLEASE TYPE YOUR NAME.]•MAKE “NM READLIST•PR (SE [HI,] :NM)The SE means SENTENCE (“print as a sentence, with spaces between the parts”). Notice the use of ( ) instead of [ ] . The [HI,] is taken literally, but the :NM prints whatever is input in the previous line.Sentences in LogoExamine the following procedure:•TO EXCHANGE•PR [PLEASE TYPE YOUR NAME.]•MAKE “NM READLIST•PR (SE [HI,] :NM)•ENDRunning the procedure:•EXCHANGE (to start the procedure)•PLEASE TYPE YOUR NAME.•MARY SMITH (entered by the user)•HI, MARY SMITHSentences in LogoNext we are going to create a procedure that puts together sentences from randomly chosen words.This procedure will (1) create lists of words (verbs, nouns, adjectives), (2) choose randomly from these lists, and (3) put the randomly chosen words into a sentence and print it.Sentences in LogoWe will use a sentence format like this:•THE adjective noun verb.•Example: THE HAPPY DOG RUNS.To make lists of words:•MAKE “VERBS [RUNS HOPS SINGS]•MAKE “NOUNS [DOG TREE CAR]•MAKE “ADJ [HAPPY GREEN BIG]Sentences in LogoTo choose randomly from these lists:• MAKE “WORD1 ITEM 1 + RANDOM 3 :ADJ •MAKE “WORD2 ITEM 1 + RANDOM 3 :NOUNS•MAKE “WORD3 ITEM 1 + RANDOM 3 :VERBSSentences in LogoThe procedure thus far…•TO RANDOM.SENTENCE•MAKE “VERBS [RUNS HOPS SINGS]•MAKE “NOUNS [DOG TREE CAR]•MAKE “ADJ [HAPPY GREEN BIG]•MAKE “WORD1 ITEM 1 + RANDOM 3 :ADJ •MAKE “WORD2 ITEM 1 + RANDOM 3 :NOUNS•MAKE “WORD3 ITEM 1 + RANDOM 3 :VERBSSentences in LogoTo put the words into a sentence, we will use PR (print) and SE (sentence).Remember that we are trying for•THE adjective noun verb.Here is the Logo printing command we will use: PR (SE [THE] :WORD1 :WORD2 :WORD3 [.])Sentences in LogoThe final version:•TO RANDOM.SENTENCE•MAKE “VERBS [RUNS HOPS SINGS]•MAKE “NOUNS [DOG TREE CAR]•MAKE “ADJ [HAPPY GREEN BIG]•MAKE “WORD1 ITEM 1 + RANDOM 3 :ADJ •MAKE “WORD2 ITEM 1 + RANDOM 3 :NOUNS•MAKE “WORD3 ITEM 1 + RANDOM 3 :VERBS•PR (SE [THE] :WORD1 :WORD2 :WORD3 [.])•ENDSentences in LogoHow could you make RANDOM.SENTENCE into a “random story”?How about…•TO RANDOM.STORY•REPEAT 5 [RANDOM.SENTENCE]•ENDAn Addition DrillLogo sentences can be constructed using numbers, too.Suppose you are trying to make an addition drill with random problems.You would have to create a procedure that (1) chooses two random numbers, (2) displays the addition problem, (3) gets the answer, (4) checks to see if it is correct, and (5) gives feedback.An Addition DrillChoosing two random numbers would look like this:•MAKE “X 1 + RANDOM 10•MAKE “Y 1 + RANDOM 10To print the problem as a sentence:•PR (SE :X [+] :Y [= ?])What would be different for a multiplication problem? For larger numbers?An Addition DrillGetting the answer would look like this:•MAKE “ANS FIRST READLISTThe procedure so far is…•TO ADD. PROBLEM•MAKE “X 1 + RANDOM 10•MAKE “Y 1 + RANDOM 10•PR (SE :X [+] :Y [= ?])•MAKE “ANS FIRST READLISTAn Addition DrillThe next step is to check the answer.The question is…how will the computer know the right answer when two random numbers were chosen?The correct answer should be :X + :Y since :X and :Y contain the chosen numbers.But how will the computer compare the student’s answer with the
View Full Document