Lesson Plan VVBasic B - Lesson 7

Overview

Lesson 7 and subsequent lessons have relatively short introductory parts. The students have now learned a powerful variety of VVBasic commands and programming concepts, and the emphasis now is on learning to use that knowledge. The only new language feature in lesson 7 is use of REM or // to indicate a comment. In this lesson and in lesson 8, students will work on the Guess A Number game.

Introductory

In this lesson students will copy and paste a sample VVBasic program from one window to another. Some students will be old hands at copying and pasting, but some will need an introduction to the clipboard concept, Ctrl-A, Ctrl-C, Ctrl-V, and right clicking for a context menu.

Computer programs are instructions for the computer, but they also have to readable by humans. That's why we indent code and use meaningful variable names. An important way to make a program understandable is to put in comments. A comment is a line of text, or part of a line, that begins with REM (for remark) or //. The computer ignores the REM or // and everything after on the same line. Usually comments are put just before the line or lines they refer to.

VVBasic commands Output
// Print out the average of X1, X2, and X3
PRINT (X1 + X2 + X3) / 3
17.2
REM This program written by Ada Byron Lovelace
REM Aug. 5, 1832
NUMBER Factor = 1.5
 

The Guess A Number game is an old favorite most students will be familiar with. One player announces that he or she is thinking of a number between two limits, say 0 and 100. The second player makes a guess, say 50. If that was the number, the first player says so. If that was not the number, the first player says "Your guess was too high" or "your guess was too low," as appropriate. The second player then makes another guess, and should be able to quickly narrow in on the first player's number.

Play the game several times with the students, with them guessing your number. Tell them that the best guess is the one exactly midway between the highest and lowest possible guesses. How do you compute the midway? The answer (which the students should be able to tell you) is (low + high) / 2. The students should recognize this as the mean or average. Faced with a range such as 50 and 75, students will often guess 60 or 65, but stress that 62 or 63 (only whole numbers are allowed, so rounding is necessary) is better, because those numbers are closer to the midpoint.

(You can amaze the students by claiming that you can guess a number between 1 and 1000 in no more than 10 guesses. This is the case because when 1000 is divided in two 10 times, the result is less than one.)

As you play the game with the students, sketch out a computer program, as in lesson 6. Don't write the program on the board -- make the students fully participate in creating it. The sketch will look something like this:

NUMBER Lowest = 1
NUMBER Highest = 100
NUMBER ComputersNumber = Random(Lowest, Highest)
NUMBER Guess
REPEAT
   INPUT "What is your guess? " Guess
   IF Guess = ComputersNumber THEN
     PRINT "That's it!"
     BREAK;
   ENDIF
   IF Guess > ComputersNumber THEN
     PRINT "Your guess was too low."
   ENDIF
   IF Guess < ComputersNumber THEN
     PRINT "Your guess was too high."
   ENDIF
ENDREPEAT

Programming Lab

As always, point the browser at http://vvbasic.csed.org to begin.

Click in the area on the web page that says Sample VVBasic Code. Click on Guess a number (human guesses). You should see a new window with a VVBasic program in it. Copy the program code from the new window to the window titled "Enter your VVBasic program here" using copy and paste. (If you don't know how to copy and paste, raise your hand.) Close the new window.

  1. The Guess a number program is now in your programming window. Run it a few times. Look at the program code and make sure you understand it.
    Many students will just want to play the game. Make sure that after a few minutes all students move on to the next step.

  2. You will now make a few changes to the program. Add a new PRINT command so that the program greets you by name. For instance, the program might start off with "Hey, Abner, try to guess the computer's number."

  3. Change the INPUT statement so that your name is in the label, like this:
    VVBasic Dialog Box

  4. Right now, the computer always chooses a number between 1 and 100. Put an INPUT statement in the program, towards the top, so that the human player can type in the values of LowestNumber and HighestNumber. Run the program and enter 300 and 400 in the dialog box. Does the program work correctly?
    Some students will just change the source code to change the initial values of LowestNumber and HighestNumber.

  5. Change the program so that instead of PRINTing "Thanks for playing!" at the end, the computer PRINTs "It took you 6 guesses!". Of course, the computer doesn't always print 6. It should put in the actual number of guesses. To do this, you'll need to
    • create a new numeric variable (perhaps called NumberOfGuesses),
    • initialize this variable to 0,
    • add 1 to this variable each time the player makes a guess,
    • change the PRINT at the end of the program.
    Make the changes, run your new program, and make sure it works.

  6. Using REM or //, put a comment before the LET statement you wrote in step 5, explaining that the program is keeping track of the number of guesses.

Concluding Questions and Answers

To do.

Evaluation Activity

To do.