19.04.2017 Views

Learn to Program with Small Basic

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

Figure 19-8 shows a sample run of the program.<br />

Figure 19-8: Sample output of the Math Wizard program<br />

TRY IT OUT 19-3<br />

Think of ways <strong>to</strong> improve the Math Wizard program, and try <strong>to</strong> implement them.<br />

For example, add some colors <strong>to</strong> make the output look fancier or draw something<br />

after each puzzle.<br />

<strong>Program</strong>ming Challenges<br />

If you get stuck, check out http://nostarch.com/smallbasic/ for the solutions<br />

and for more resources and review questions for teachers and students.<br />

1. Let’s write a spelling quiz game using homonyms. Homonyms are<br />

words that sound the same but have different meanings. Use Notepad<br />

<strong>to</strong> create the following text file:<br />

In your math class;ad/add;add<br />

Halloween queen;which/witch;witch<br />

Eyes do this;sea/see;see<br />

In the church;altar/alter;altar<br />

A rabbit;hair/hare;hare<br />

A good s<strong>to</strong>ry;tail/tale;tale<br />

Animals have them;clause/claws;claws<br />

Pencils do this;right/write;write<br />

Each line in this file contains three fields separated by semicolons.<br />

The first field is the hint you’ll show the player, such as In your math<br />

class. The second field is the two possible answers that your player will<br />

choose from, such as ad/add. The third field is the correct answer, such<br />

as add.<br />

In each round, have the program display the hint and the two possible<br />

answers <strong>to</strong> the player, and then wait for them <strong>to</strong> enter their answer.<br />

Have the program compare the user’s answer <strong>with</strong> the correct answer,<br />

and then let them know whether their answer is correct.<br />

Receiving File Input and Output 305

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!