Caleb Cushing's Blog

Teaching Perl - Week 2

Guess the Number

So our little game is exceptionally mediocre and not very fun. Head First Programming: A Learner's Guide to Programming Using the Python Language suggests we give the game player some help by telling them if the number is lower or Higher.

This game is a lot less challenging, and still not very good. So one one of the problems is it exits instead of letting it try till we get it right. So lets change that.

This one is going to show your students something they likely haven’t seen before. An until statement. until is logically equivalent to while not or in this case you could write while ( $guess != 5 ) {…} and have it do exactly the same thing.

We’ve got some bad practices in this code it lacks useful comments and uses a magic number. Also the instructions aren’t clear if you don’t know how the program is written. Let’s add that stuff next.
So this code is too simple for comments, but I’ve tried to show what I think some good ones would be. Usually ‘novice’ books say use meaningful comments but don’t show any. Students then end up writing comments like # initialize local variable guess to 0. That’s not useful the code obviously says that. Instead comments should say, what a larger chunk of is trying to do, or why it’s doing it that way.

In Head First Programming: A Learner's Guide to Programming Using the Python Language the author uses the variable secret where I’ve used $winning_num because he ends up using a random number generator (we will in a moment), however, this is not a very descriptive name. $winning_num is a better name because it’s more descriptive of what the number actually is. He also skips removing the ‘magic numbers’ step which is probably why the variable name used is poor.

So let’s make the winning number random.

There we go… Now are number is random. … … … notice the logic error yet? no? go read the rand documentation. Got it now? ok simply put this generates 10 total possible random numbers starting at 0, e.g 0-9. Our code isn’t broken but it’s not doing what we want. But the docs don’t say how to generate a range… how can I do that? run perldoc -q random this will return perlfaq4 we’re looking for How do I get a random number between X and Y?. That tells us how to fix it… our code should now look something like.

we could just put 10 in rand in this case but that’s not as flexible. You’ll also note I commented on why we did it that way. Kind of a way of making sure no one goes back to the other way, it also tells people why we did it this way.

If you’ve used another language you might ask, “Why not use a switch case?”. Most instructors and books say “perl doesn’t have a switch case”. Even perl intro still falsely states that
“Perl has most of the usual conditional and looping constructs except for case/switch (but if you really want it, there is a Switch module in Perl 5.8 and newer, and on CPAN.”
. That’s even worse! has been in core for a while now… but is deprecated an you should NOT use it. since perl 5.10.0 perl has had a Switch statement, so let’s rewrite our code using that.

Note the use of use feature ‘switch’; make sure you use this instead of use Switch; which will use the now deprecated module.

You can shorten use feature ‘say’; use feature ‘switch’; to use feature qw(say switch);.

Beside’s introducing switch/case this introduces the default argument $
and smart matching.

You’ll also note that I changed the algorithm but my comments didn’t change.

As you can see TIMTOWTDI.


What else is wrong with our game? how could we fix it? How could we prevent the problem from being reintroduced? How can we be sure that our random problem is actually fixed? and make sure that it’s not reintroduced? Can you think of any other ways we can write this program? or any other improvements? write the program so that it has 3 difficulty levels easy, medium, hard with ranges 1-10,1-100,1-1000 respectively. Blog these to Iron Man.

Optional: if you have Programming: Principles and Practice Using C++ have your students do Drill 1 from chapter 3 (obviously in perl not c++).


comments powered by Disqus