430 likes | 592 Views
Logo Lesson 5. TBE 540-40 Fall 2004 Farah Fisher. Prerequisites. Given 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. Objectives.
E N D
Logo Lesson 5 TBE 540-40 Fall 2004 Farah Fisher
Prerequisites • Given 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.
Objectives • Put 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 Lists • A 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 Lists • Here 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 Lists • PR 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 :WORD1
Logo Lists • Logo 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 :PRIME
Sentences in Logo • You 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 Logo • If 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 Logo • The commands to ask for and get the name would be • PR [PLEASE TYPE YOUR NAME.] • MAKE “NM READLIST • If 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 Logo • Take 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 Logo • Examine the following procedure: • TO EXCHANGE • PR [PLEASE TYPE YOUR NAME.] • MAKE “NM READLIST • PR (SE [HI,] :NM) • END • Running the procedure: • EXCHANGE (to start the procedure) • PLEASE TYPE YOUR NAME. • MARY SMITH (entered by the user) • HI, MARY SMITH
Sentences in Logo • Next 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 Logo • We 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 Logo • To 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 :VERBS
Sentences in Logo • The 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 :VERBS
Sentences in Logo • To 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 Logo • The 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 [.]) • END
Sentences in Logo • How could you make RANDOM.SENTENCE into a “random story”? • How about… • TO RANDOM.STORY • REPEAT 5 [RANDOM.SENTENCE] • END
An Addition Drill • Logo 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 Drill • Choosing two random numbers would look like this: • MAKE “X 1 + RANDOM 10 • MAKE “Y 1 + RANDOM 10 • To print the problem as a sentence: • PR (SE :X [+] :Y [= ?]) • What would be different for a multiplication problem? For larger numbers?
An Addition Drill • Getting the answer would look like this: • MAKE “ANS FIRST READLIST • The procedure so far is… • TO ADD. PROBLEM • MAKE “X 1 + RANDOM 10 • MAKE “Y 1 + RANDOM 10 • PR (SE :X [+] :Y [= ?]) • MAKE “ANS FIRST READLIST
An Addition Drill • The 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 correct answer?
An Addition Drill • The IF command is part of all programming languages. • It asks the computer to make a judgment about whether something is true or false. • In this case the student’s answer will either be the same as the computer’s or not the same - the “equality” will either be true (equal) or false (not equal).
An Addition Drill • The IF statement also has to tell Logo what to do if the judgment is true. • In this case, we will tell Logo to print the word RIGHT if the student’s answer is the same as the computer’s. • Here is the command: • IF :ANS = :X + :Y [PR [RIGHT]] • An explanation follows on the next slide.
An Addition Drill IF :ANS = :X + :Y [PR [RIGHT]] • :ANS contains the student’s answer. • :X + :Y is the correct answer • If the two are the same (equality is true), then whatever is inside the [ ] is done. • In this case, PR [RIGHT] is inside the [ ]. • Notice that one set of [ ] is for the IF statement and the other is part of PR.
An Additional Drill • Here’s the completed procedure. • TO ADD.PROBLEM • MAKE “X 1 + RANDOM 10 • MAKE “Y 1 + RANDOM 10 • PR (SE :X [+] :Y [= ?]) • MAKE “ANS FIRST READLIST • IF :ANS = :X + :Y [PR [RIGHT]] • END
An Addition Drill • But wait…wouldn’t it be better if we gave them some feedback when they didn’t get the right answer? • There is an IFELSE statement in Logo that allows you to tell the computer what to do if the IF comparison is true AND what to do otherwise.
An Addition Drill • IFELSE comparison [actions if true] [actions if false] • For our procedure, it might look like this: • IFELSE :ANS = :X + :Y [PR [RIGHT]] [PR [WRONG]] OR • IFELSE :ANS = :X + :Y [PR [RIGHT]] [PR (SE [NO, THE ANSWER IS] :X + :Y)]
An Addition Drill • Here it is with two kinds of feedback: • TO ADD.PROBLEM • MAKE “X 1 + RANDOM 10 • MAKE “Y 1 + RANDOM 10 • PR (SE :X [+] :Y [= ?]) • MAKE “ANS FIRST READLIST • IFELSE :ANS = :X + :Y [PR [RIGHT]] [PR (SE [NO, THE ANSWER IS] :X + :Y)] • END
As Addition Drill • One more thing…the ADD.PROBLEM procedure prints only one problem. • Shouldn’t a drill have more than one problem? • How about this… • TO ADD.DRILL • REPEAT 10 [ADD.PROBLEM] • END
Recursion • Recursion really means repetition, but it is not the same as REPEAT. • In Logo , it usually refers to using the name of a procedure inside the same procedure. • For a detailed discussion of recursion, see http://www.csudh.edu/fisher/tbe540/LHO5A.htm
Self Check Lesson 5 • What will be printed in the text window after the commands below are entered? • MAKE “COLORS [RED GREEN BLUE] • PR :COLORS • COLORS • RED GREEN BLUE • Depends on which color is chosen.
Self Check Lesson 5 • What will be printed in the text window after the commands below are entered? • MAKE “COLORS [RED GREEN BLUE] • PR :COLORS • COLORS • RED GREEN BLUE • Depends on which color is chosen.
Self Check Lesson 5 • What will be printed in the text window after the commands below are entered? • MAKE “COLORS [RED GREEN BLUE] • PR ITEM 2 :COLORS • GREEN • RED GREEN BLUE • Depends on which color is chosen.
Self Check Lesson 5 • What will be printed in the text window after the commands below are entered? • MAKE “COLORS [RED GREEN BLUE] • PR ITEM 2 :COLORS • GREEN • RED GREEN BLUE • Depends on which color is chosen.
Self Check Lesson 5 • What will be printed in the text window if the commands below are entered? • MAKE “DOGS [BEAGLE PIT BULL] • MAKE “WORD2 ITEM 2 :DOGS • PR “WORD2 • BEAGLE PIT BULL • PIT BULL • PIT
Self Check Lesson 5 • What will be printed in the text window if the commands below are entered? • MAKE “DOGS [BEAGLE PIT BULL] • MAKE “WORD2 ITEM 2 :DOGS • PR “WORD2 • BEAGLE PIT BULL • PIT BULL • PIT {“items” are separated by a space}
Self Check Lesson 5 • What will be printed in the text window if the commands below are entered? • MAKE “X 27 • IF :X > 5 [PR [TOO BIG!]] • Nothing will be printed. • TOO BIG • PR [TOO BIG]
Self Check Lesson 5 • What will be printed in the text window if the commands below are entered? • MAKE “X 27 • IF :X > 5 [PR [TOO BIG!]] • Nothing will be printed. • TOO BIG • PR [TOO BIG]
Self Check Lesson 5 • What will be printed in the text window if the commands below are entered? • MAKE “BB 100 • IFELSE :BB > 100 [PR [yes]] [PR [no]] • Nothing will be printed. • yes • no
Self Check Lesson 5 • What will be printed in the text window if the commands below are entered? • MAKE “BB 100 • IFELSE :BB > 100 [PR [yes]] [PR [no]] • Nothing will be printed. • yes • no {:BB=100, so it is NOT greater than 100}
Logo Lesson 5 • Try the activities posted under Lesson 5 on the class website.