# guessing script - PERL Beginners

Learning Perl here.. So I wrote this number guessing script but can't see what is wrong with it. I can never get the solution even when I know it's correct. What am I doing wrong? #!/usr/bin/env perl use Math::Random; use strict; sub run(){ my \$solution = random_uniform_integer(1, 1, 100); my \$guess = undef; do { print "\\$solution = \$solution\n"; print "Guess (1 - 100) ?\n"; chomp(my \$guess = <STDIN>); if (\$guess > \$solution) { print "\$guess is too HIGH!\n"; } if (\$guess < \$solution) { print "\$guess is too LOW!\n"; } } while (\$guess != \$solution); print "\$guess is correct, ...

1. ## guessing script

Learning Perl here.. So I wrote this number guessing script but can't see
what is wrong with it. I can never get the solution even when I know it's
correct. What am I doing wrong?

#!/usr/bin/env perl

use Math::Random;
use strict;

sub run(){
my \$solution = random_uniform_integer(1, 1, 100);
my \$guess = undef;
do {
print "\\$solution = \$solution\n";
print "Guess (1 - 100) ?\n";
chomp(my \$guess = <STDIN>);
if (\$guess > \$solution) {
print "\$guess is too HIGH!\n";
}
if (\$guess < \$solution) {
print "\$guess is too LOW!\n";
}
} while (\$guess != \$solution);
print "\$guess is correct, YAY!\n";
print "Play again? ";
chomp(my \$answer = <STDIN>);
if (lc(\$answer) eq 'y') {
run();
}
}

run();

TIA :)

--
Greg Donald
com
Greg Guest

2. ## RE: guessing script

(my \$guess = <STDIN>); 'my' is your problem.. that creates new varialbe remove it..
HTH
Jay

-----Original Message-----
From: Greg Donald [mailto:com]
Sent: Thursday, April 15, 2004 11:41 AM
To: org
Subject: guessing script

Learning Perl here.. So I wrote this number guessing script but can't see
what is wrong with it. I can never get the solution even when I know it's
correct. What am I doing wrong?

#!/usr/bin/env perl

use Math::Random;
use strict;

sub run(){
my \$solution = random_uniform_integer(1, 1, 100);
my \$guess = undef;
do {
print "\\$solution = \$solution\n";
print "Guess (1 - 100) ?\n";
chomp(my \$guess = <STDIN>);
if (\$guess > \$solution) {
print "\$guess is too HIGH!\n";
}
if (\$guess < \$solution) {
print "\$guess is too LOW!\n";
}
} while (\$guess != \$solution);
print "\$guess is correct, YAY!\n";
print "Play again? ";
chomp(my \$answer = <STDIN>);
if (lc(\$answer) eq 'y') {
run();
}
}

run();

TIA :)

--
Greg Donald
com

--
To unsubscribe, e-mail: org
For additional commands, e-mail: org
<http://learn.perl.org/> <http://learn.perl.org/first-response>

Jayakumar Guest

3. ## Re: guessing script

In a message dated 4/15/2004 12:09:40 PM Eastern Daylight Time,
com writes:
Learning Perl here.. So I wrote this number guessing script but can't see
what is wrong with it. I can never get the solution even when I know it's
correct. What am I doing wrong?
[Snipped Code]

It has to do with the scope of guess. You used my twice. Get rid of that my
in "chomp(my \$guess = <STDIN>); " and then it's good. The \$guess being
evaluated in the while() condition is undef because that is what it has been defined
to be in that scope. By saying "chomp(my \$guess = <STDIN>);" you're saying
that this is kind of a new \$guess specific to that block, and it doesn't even
exist outside of it.

-will
(the above message is double rot13 encoded for security reasons)

Most Useful Perl Modules
-strict
-warnings
-Devel::DProf
-Benchmark
-B::Dep
-Data::Dumper
-Clone (a Godsend)
-Perl::Tidy
-Beautifier

WilliamGunther@aol.com Guest

4. ## Re: guessing script

On Apr 15, 2004, at 10:40 AM, Greg Donald wrote:

Looks like you already got some good answers. Let me add a few minor

You should add a:

use warnings;

I believe this would have even caught your previous mistake.

Don't declare subroutines like that. Use:

sub run { # no parens

The above should probably be:

elsif (\$guess < \$solution) {

Move the rest of this subroutine outside, into the general script
(around your run(); call). There's no need to use recursion for this.

James

James Guest

5. ## Re: guessing script

On Thursday 15 April 2004 12:43 pm, James Edward Gray II wrote:

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? ";
} while (lc(\$answer) eq 'y');

--
Greg Donald
com
Greg Guest

6. ## Re: guessing script

On Apr 15, 2004, at 1:24 PM, Greg Donald wrote:

>
> Thanks for you comments. Is this the best way then? Seems to work,
> but I'm
> curious what could be any better..[/ref]

I think those changes are for the better, yes.

James

James Guest

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•