Re: Simple loop question
- From: shawnhcorey@xxxxxxxx (Mr. Shawn H. Corey)
- Date: Wed, 31 May 2006 12:13:09 -0400
On Wed, 2006-31-05 at 17:58 +0200, Danny wrote:
Hi list,
Hope this is not too simple or a stupid question:
I have a slight problem with a loop. It is a simple numbers guessing game. It
works fine, but when I run this script, it gives me "Too low" immediately after
the prompt. What should I do to get the last "else" statement displayed first?
Following is the code:
#!/usr/bin/perl -w
use strict;
use warnings;
$upper = 20 ;
$lower = 1 ;
$target = 11 ;
my $upper = 20;
my $lower = 1;
my $target = 11;
my $guess = <STDIN>;
while ( )--
{
if ( $guess > $target )
{
print "Too high\n" ;
$guess = <STDIN> ;
}
elsif ( $guess < $target )
{
print "Too low\n" ;
$guess = <STDIN> ;
}
elsif ( $guess == $target )
{
print "You guessed right\n" ;
exit ;
}
else
{
print "Please choose a number between ${lower} and ${upper}\n" ;
$guess = <STDIN> ;
}
}
Thank you in advance
Danny
__END__
Just my 0.00000002 million dollars worth,
--- Shawn
"For the things we have to learn before we can do them, we learn by doing them."
Aristotle
* Perl tutorials at http://perlmonks.org/?node=Tutorials
* A searchable perldoc is at http://perldoc.perl.org/
.
- References:
- Simple loop question
- From: Danny
- Simple loop question
- Prev by Date: Simple loop question
- Next by Date: faster search engine for fulltext search
- Previous by thread: Simple loop question
- Next by thread: Re: Simple loop question
- Index(es):
Relevant Pages
|
|