Simple loop question



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
.



Relevant Pages

  • Re: Simple loop question
    ... 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. ...
    (perl.beginners)
  • Re: User Input Question
    ... point where the user will gain control? ... to loop to handle configuration for multiple machines? ... need to ask your questions inside the loop. ... "The only stupid question is the one you don't ask." ...
    (comp.lang.tcl)
  • Re: Loop doesnt behave the way its supposed to.
    ... > Hi folks, ... A loop doesn't behave the way I'd like to (namely, ... Probably stupid question, since I was too lazy to work out the ...
    (comp.lang.c)
  • Re: Find And Replace Text String VBA
    ... This might be a stupid question, but given that the Table name is ADDRESS and ... the Field name is STREETNAME. ... I'm sure it relates to the strOriginalValue portion, ...
    (microsoft.public.access.modulesdaovba)
  • Re: Simple loop question
    ... I have a slight problem with a loop. ... It is a simple numbers guessing game. ... works fine, but when I run this script, it gives me "Too low" immediately after ... print "You guessed right\n" and exit; ...
    (perl.beginners)