#!/usr/bin/perl use strict; use warnings; my $low = 100; #Current low limit my $high = 1000; #Current high limit # UPDATED: Thanks to jdporter pointing out the necessity for a # perfect $goal I changed this algorithm to make it work every time. my $goal = int(rand($high-$low+1))+$low; #added +1 in case $goal was 0 while (1) { print "Enter a number between $low and $high: "; #The answer from the user my $answer = ; chomp($answer); if ($answer =~ /\D/) { # UPDATED D from d+ -> from toolic 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; } }