dj_jodo has asked for the wisdom of the Perl Monks concerning the following question:
I am doing the guess a number game I have it running correctly the only thing I can not figure out is how to track the numbers guessed. It is suppose to keep track of the numbers the user guess and print an error message if they guess the same number more than once. This is what I have so far:
do { @numbers=(1..10); $index=int (rand (10)); $rannumber="$numbers[$index]\n"; game($rannumber); print "Would you like to play again(Y or N) "; do { chomp($answer=<STDIN>); $answer=uc($answer); if (($answer ne "Y") and ($answer ne "N")){ print "Invalid entry - PLEASE SELECT Y OR N\n"; } }while (($answer ne "Y") and ($answer ne "N")); } until ($answer eq "N"); sub game { my $rannumber=@_[0]; print "Guess a number between (1-10) "; do { my $guess=<STDIN>; @allguess=$guess; $track = grep(!/1..10/, $allguess); if ($track = $allguess) { print "Same guess try again\n"; } if ($guess > 10) { print "Invalid entry - Guess Again\n"; } elsif ($guess < 1) { print "Invalid entry - Guess Again\n"; } elsif ($guess == $rannumber) { print "You Win\n";} else {print "Guess Again\n";} } until ($guess == $rannumber); return; }
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Help with Guessing Game
by choroba (Cardinal) on May 11, 2013 at 13:11 UTC | |
by davido (Cardinal) on May 11, 2013 at 22:49 UTC | |
by dj_jodo (Initiate) on May 11, 2013 at 13:30 UTC | |
by ww (Archbishop) on May 11, 2013 at 13:45 UTC | |
by Anonymous Monk on May 11, 2013 at 14:07 UTC | |
|
Re: Help with Guessing Game
by BillKSmith (Monsignor) on May 12, 2013 at 03:47 UTC | |
by davido (Cardinal) on May 12, 2013 at 04:28 UTC | |
by BillKSmith (Monsignor) on May 12, 2013 at 13:14 UTC |