#!/usr/bin/perl # Tom Blum # May 2007 # IWA-HWG Homework/ Instructor M. Kowalski print "\nThis program selects a number between 1 and 100 \n"; print "\"at random.\" You will then be prompted to enter \n"; print "a guess to which the program will respond informing \n"; print "you whether the number is lower or higher than your\n"; print "guess or whether you have guessed correctly. If you\n"; print "wish to end the game type \"quit\" or \"exit\".\n"; # use a pseudo-random number generator to pick a number between 1 and 100 $the_number = int(1 + rand 100); #print $the_number; #printed number during debugging stages # initialize the variable counting the user's guess to zero $guess_counter = 0; # make the "fake" guess $guess = -1; # prompt the user for guesses until the answer is correct while($guess != $the_number) { #prompt for and read in guess print "\nEnter your guess: "; chomp($guess = ); # converts user's input into lower case $guess = lc $guess; if($guess eq "quit" || $guess eq "exit" || $guess eq "") { print "\nBye bye."; last; } # determines if the user guessed a number and # if that number is in the proper range if($guess<1 || $guess>100) { print "\nPlease enter a number bewteen 1 and 100 (e.g. 57): "; next; } $guess_counter++; #compare guess to system generated number if($the_number<$guess) { print "\nIt\'s lower than $guess.\n"; } elsif($the_number>$guess) { print "\nIt\'s higher than $guess.\n"; } else { print "\nYou got it in $guess_counter guesses!\n"; } }