This a very simple script that helped me learn a little more about Perl and helped my kids
visualize ordering of numbers.
**UPDATE: Reconsidered everything, went back and rethought the code
and this is the latest idea I have come up with, tell me what
you think
#!/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 = <STDIN>;
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;
}
}
The early bird gets the worm but the second mouse gets the cheese.
pretendeavor
Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
Read Where should I post X? if you're not absolutely sure you're posting in the right place.
Please read these before you post! —
Posts may use any of the Perl Monks Approved HTML tags:
- a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
| |
For: |
|
Use: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.