Well, I finally managed to make a script with no help from others! It is a silly "Guess the Random Number" game with no use except the experience of making it. If anyone wants to advise me of better ways to do the things I did, please feel free to respond!
The real reason for the post is that I can't understand why the subroutine was doing something. The second line of the sub is now
my $numb=$_[0]; and works fine, but when it was
$numb=@_; it always had 1 as the value no matter what was passed. Why was that so? What was happening?
TIA
jg
#!/usr/local/bin/perl -w
use strict;
srand;
print "I have a number between 1 and 19.\nTry to guess it.\n";
my $number=int(rand(20));
guess_routine($number);
sub guess_routine {
my @guesses;
my $numb=$_[0];
print "Guess?\n";
my $in=<STDIN>;
chomp ($in);
print "You said $in is the number.\n";
while ($in != $numb){
if ($in > $numb) {
print "Too high! ";
}else{
print "Too low! ";
}
push (@guesses, $in);
@guesses = sort{$a <=> $b} @guesses;
print "\nSo far you've guessed @guesses.";
print "\nWhat is your guess?\n";
$in=<STDIN>;
chomp ($in);
}
print "\nYes, $numb is the number!\n";
print "So you want another go?\n";
chomp(my $answer=<STDIN>);
if ($answer =~/^y/i){
@guesses="";
print "\nI have a new number between 1 and 19.\nTry to guess i
+t.\n";
$number=int(rand(20));
guess_routine($number);
}else{
print "\nOK, bugger off then!\n";
exit 0;
}
}
_____________________________________________________Ain't no time to hate! Barely time to wait! ~RH
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.