Beefy Boxes and Bandwidth Generously Provided by pair Networks
Just another Perl shrine
 
PerlMonks  

Number guessing game

by Anonymous Monk
on Nov 26, 2014 at 01:49 UTC ( [id://1108410]=perlquestion: print w/replies, xml ) Need Help??

Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

I have everything working except I can not figure out a way to get it to say all of the previous guesses I can only get the last one. I've looked in all of the resource material I have and am missing it. Any help is appreciated

#!/usr/bin/perl use Modern::Perl; my $target = (int rand 100) + 1; my $number = 99 ; GUESS: foreach my $guess_num (1..$number) { say "($guess_num) Please enter a guess:"; my $guess = <>; chomp $guess; unless ($guess =~ /^\d+$/) { say "This guess is out of range. Guess between 1 and 100!"; redo GUESS; } if ($guess == $target) { say "Congrats, you guessed right!"; last GUESS; } elsif ($guess < $target) { say "Your guess of $guess was too small."; say "your previous guesses:$guess,"; } elsif ($guess > $target) { say "Your guess of $guess was too big."; } if ($guess_num == $number) { say "You have used all your guesses. Please try again."; last GUESS; } }

Replies are listed 'Best First'.
Re: Number guessing game
by Athanasius (Archbishop) on Nov 26, 2014 at 02:01 UTC

    To keep a record of all the (valid) guesses made, use an array:

    my @guesses; GUESS: ...

    Then, once a guess has been validated, push it onto the array:

    push @guesses, $guess;

    When you want to print all the guesses made so far, you can say:

    print join(', ', sort { $a <=> $b } @guesses), "\n";

    Hope that helps,

    Athanasius <°(((><contra mundum Iustus alius egestas vitae, eros Piratica,

      push is too suspicious, I'd say the OP is supposed to do something like $numbers[ $guess_num - 1 ] = $guess
Re: Number guessing game
by toolic (Bishop) on Nov 26, 2014 at 13:26 UTC
    Here is an enhancement: you can use a hash to show all guesses and to notify the user that a guess was already used. I also fixed your range check to match your printed advice (1 to 100). Finally, I used perltidy.
    use Modern::Perl; my $target = ( int rand 100 ) + 1; my $number = 99; my %guesses; GUESS: foreach my $guess_num ( 1 .. $number ) { say "($guess_num) Please enter a guess:"; my $guess = <>; chomp $guess; if ( ($guess =~ /\D/) or ($guess > 100) or ($guess == 0) ) { say "This guess is out of range. Guess between 1 and 100!"; redo GUESS; } if ( $guess == $target ) { say "Congrats, you guessed right!"; last GUESS; } elsif ( exists $guesses{$guess} ) { say "You already guessed $guess."; } elsif ( $guess < $target ) { say "Your guess of $guess was too small."; } elsif ( $guess > $target ) { say "Your guess of $guess was too big."; } $guesses{$guess}++; print "your previous guesses: ", join ' ', sort {$a <=> $b} keys % +guesses; print "\n"; if ( $guess_num == $number ) { say "You have used all your guesses. Please try again."; last GUESS; } }

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://1108410]
Approved by Athanasius
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others admiring the Monastery: (5)
As of 2024-04-23 16:30 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found