in reply to Re^2: Guess That Number
in thread Guess That Number
Here is the code that works for me. Note that it does not have jdporter's bug fix.
use strict; use warnings; my $low = 1; #Current low limit my $high = 1000; #Current high limit #This is the secret number...Add one because we #don't need to wind up with a zero now do we? my $goal = int(rand($high))+1; while (1) { print "Enter a number between $low and $high: "; #The answer from the user my $answer = <STDIN>; chomp($answer); if ($answer =~ /\D/) { print "Please enter a number only\n"; next; } if ($answer == $goal) { print "Holy cow! You guessed it!\n"; exit; } if (($answer < $low) || ($answer > $high)) { print "Please stay between $low and $high.\n"; next; } if ($answer < $goal) { $low = $answer; } else { $high = $answer; } }
|
|---|