Handout 12B

The purpose of this exercise is to practice using the READWORD and READLIST commands in LOGO.

So far, all of the LOGO programs we have written have been designed to run without any human intervention. Except for these, almost all the computer programs we've ever used (including LOGO itself!) demand user input in order to run. For example, you web browser is useless if you don't give it instructions by typing or using your mouse.

We won't introduce pull-down menus and mouse events in this course, but we can use old-fashioned keyboard input to control our programs. Jenny's FINDFRIENDS program on page 182 is an example of a program that requires keyboard input. Type in the following modified version of the program and try it out.

TO FAVORITEFOOD
  PRINT []
  PRINT [What is your favorite food?]
  MAKE "FOOD READLIST
  IFELSE :FOOD = [ice cream] [
    PRINT [Ice cream is my favorite food too!]
    STOP
   ] ~
   [
    PRINT (SENTENCE [I like] :FOOD [too!])
    FAVORITEFOOD
   ]
END
The READLIST command takes whatever text you enter turns it into a list and outputs it. In this program, the MAKE command is used to assign the output of READLIST to the variable :FOOD. Once this is done we can use the variable :FOOD with familiar commands like FIRST, BUTFIRST and IF. (For an extra challenge, try using READLIST with the commands MEMBER? and LISTTOARRAY described in the user manual!)

To practice using IFELSE and the output of the READLIST command, make a few changes to the FAVORITEFOOD program. You could add a food that your procedure hates or add some different foods it particularly likes. If you're not interested in food, change the subject of the procedure; you could write a program to discuss sports or the weather!


It is impossible to anticipate all the possible responses to the question "What is your favorite food?" It is easier to write an interesting and useful computer program if you restrict the possible inputs to that program in some way. A time-honored way of doing this is to present the user of a program with a menu of choices and ask them to choose one option from the menu. In this case you can assume that the input will be a single word or digit. To read a single "word" rather than a list, use the LOGO command READWORD. A simple example is shown below:

TO ADDTWONUMBERS
  PRINT [Enter the first number to add.]
  MAKE "A READWORD
  PRINT [Enter the second number to add.]
  MAKE "B READWORD
  PRINT (SENTENCE [The sum is] :A+:B)
END
So far READWORD looks pretty dull, but it all depends on how it's used. The less simple example shown below is the beginning of an adventure game. Read through it and see if you can "win" the game on your first attempt. (Don't type it in by hand! Copy it from a web browser and paste it into the LOGO editor.) When you're tired of playing the game, modify it to suit your mood (you could even use the turtle to add illustrations) or write your own game.
TO HAUNTEDHOUSE
  MAKE "KNIFE 0
  PRINT []
  PRINT [Your new kitten has run into an abandoned house.]
  PRINT [You can hear it mewing from somewhere inside the]
  PRINT [dilapidated structure.  You try the door and it]
  PRINT [is not locked.  Do you:]
  PRINT [1. Go inside.]
  PRINT [2. Buy a new kitten.]
  MAKE "ANSWER READWORD
  IFELSE :ANSWER = 1 [
    PRINT []
    PRINT [The door creaks as you open it, and dust sifts down from]
    PRINT [the ceiling onto your head.  The house is eerily silent.]
    ENTRYWAY
   ] ~
   [LOSTKITTEN]
END

TO LOSTKITTEN
  PRINT []
  PRINT [You have lost your kitten and you must buy a new one.]
  PRINT [Play again?]
  PRINT [1. Yes.]
  PRINT [2. No.]
  MAKE "ANSWER READWORD
  IF :ANSWER = 1 [HAUNTEDHOUSE]
END

TO ENTRYWAY
  PRINT []
  PRINT [You go into the entryway and see three doors in front of you.]
  PRINT [Which will you choose?]
  PRINT [1. Kitchen.]
  PRINT [2. Library.]
  PRINT [3. Attic.]
  MAKE "ANSWER READWORD
  IFELSE (:ANSWER = 1) [KITCHEN] ~
    [ IFELSE (:ANSWER = 2) [DININGROOM] ~
      [ IFELSE (:ANSWER = 3) [ATTIC] [LOSTKITTEN]]]
END

TO KITCHEN
  PRINT []
  PRINT [You have found a knife!]
  MAKE "KNIFE :KNIFE+1
  ENTRYWAY
END

TO DININGROOM
  PRINT []
  PRINT [You are attacked by a rat!]
  IFELSE :KNIFE>0 [
    PRINT [You kill the rat with the knife, losing the knife!]
    MAKE "KNIFE :KNIFE-1
    ENTRYWAY
   ] ~
   [
    PRINT [You flee the house in terror!]
    LOSTKITTEN
   ]
END

TO ATTIC
  PRINT []
  PRINT [You see your kitten in the talons of a huge owl!]
  IFELSE :KNIFE>0 [
    PRINT [You stab the owl with your knife and the kitten leaps]
    PRINT [free and scampers down the stairs.  You run after it.]
    PRINT [Once you and the kitten are safe at home, you decide]
    PRINT [to write a computer program about your adventure.]
   ] ~
   [
    PRINT [As you watch, helpless, the owl flies away with the kitty!]
    LOSTKITTEN
   ]
END


Mtht420