On Thursday 15 April 2004 12:43 pm, James Edward Gray II wrote:
> Looks like you already got some good answers. Let me add a few minor
> comments...
Thanks for you comments. Is this the best way then? Seems to work, but I'm
curious what could be any better..
#!/usr/bin/env perl
use Math::Random;
use warnings;
use strict;
my $answer = undef;
sub run {
my $solution = random_uniform_integer(1, 1, 100);
my $guess = undef;
my $tries = 0;
do {
print "Guess (1-100)?\n";
chomp($guess = <STDIN>);
$tries++;
if ($guess > $solution) {
print "$guess is too HIGH!\n";
} elsif ($guess < $solution) {
print "$guess is too LOW!\n";
}
} while ($guess != $solution);
my $try_word = $tries == 1 ? "try" : "tries";
print "$guess is correct, YAY! It only took you $tries $try_word!\n";
}
do {
run();
print "Play again? ";
chomp($answer = <STDIN>);
} while (lc($answer) eq 'y');
--
Greg Donald
[EMAIL PROTECTED]
--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
<http://learn.perl.org/> <http://learn.perl.org/first-response>