Simple loop question
- From: danny@xxxxxxxxxxxxxx (Danny)
- Date: Wed, 31 May 2006 17:58:53 +0200
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
$upper = 20 ;
$lower = 1 ;
$target = 11 ;
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
.
- Follow-Ups:
- Re: Simple loop question
- From: DJ Stunks
- Re: Simple loop question
- From: Lawrence Statton
- Re: Simple loop question
- From: Mr. Shawn H. Corey
- Re: Simple loop question
- Prev by Date: Re: Problem moving from Data::Dumper to YAML
- Next by Date: Re: Simple loop question
- Previous by thread: hash and array question
- Next by thread: Re: Simple loop question
- Index(es):
Relevant Pages
|