30 PRINT 40 PRINT "VOUR GUESS"; 50 INPUT GUESS 60 PRINT 70 IF GUESS=SECRETNUM THEN PRINT "VOU GOT IT!": END 80 IF GUESS(SECRETNUM THEN PRINT "TOO LOW. TRV tlGtlIN. " : GOTO 40 'JO IF GUESS)SECRETNUM THEN PRINT "TOO HIGH. TRV tlGtlIN. " : GOTO 40 Lin es 80 and 90 eva lu ate the guess as greater than or less than the secret number. The PRI NT statement provides a hint that the next guess should be higher or lower. The GOTO commands in lines 80 and 90 create an infinite loop if you continue to guess incorrectly. Ending the Program The Number Quiz is programmed to stop on ly when you discover the secret number. When you enter the correct answer, line 70 gives the computer the instruction to end.
EN D stops the program, and the Ready prompt appears on your screen. Trapping Errors If you accidentally enter a letter instead of a number for GUESS, the computer sends an Error message, and the program ends abruptly. Make an intentional error by typing a letter key or pressing the I Return I key only. To avoid ending the program, you can use a TRAP command to trap the Error message. Add the lin es below and run the program again: 45 TRtlP 100 100 PRINT: PRINT" PLEtlSE ENTER tl NUMBER ONL V . " 110 GOTO 30 In line 45, the TRAP command tells the computer not to stop the program when a mistake is entered and sends the computer to line 100.
Line 100 tells the computer to print the directions for correcting the mistake. Line 110 returns the computer to the place where it left off. The TRAP statement always comes before the I N PUT statement, and it always contains the number of the line that will resolve the problem. Quiz Writing with IF-THEN A program can easi ly provide hints when the correct answer is a number, including a date. Th e following program uses IF-THEN statements and the TRAP command to evaluate guesses: 56