I remember doing this game when I was first learning in BASIC. And then the corrolary - you pick the number, the computer guesses it. I learned binary searches that day, without realising it. ;-)

Comments:

With all that in mind, here's your code with all of these changes. If you have any questions, don't hesitate to ask.

#Guessing Game #You only get 10 tries! #Gives the option of starting over. #This version should count your turns for you, down from ten. use strict; use warnings; #This is how many guesses you get. my $max_guesses = 10; sub game { #This is the number they have to guess: a random integer. my $value = int(rand(200)); print "Pick a number between 0 and 200.\n"; for (my $tries = $max_guesses; $tries; --$tries) { print "You have ", $tries, " guesses left.\n"; my $guess = <STDIN>; if($guess>$value) { print "You need to guess a lower number.\n"; print "Guesses left: ", $tries, "\n"; } elsif($guess<$value) { print "You need to guess a higher number.\n"; print "Guesses left: ", $tries, "\n"; } else { print "You guessed my number in ", $max_guesses - $tries, " guesses. Thanks for playing. \n"; return; } } print "Sorry, you ran out of guesses. My number was ", $value, ". +\n"; } my $again = 'y'; while ($again eq 'y') { game(); print "Play Again? (y/n) \n"; $again = <STDIN>; chomp ($again); } print "Goodbye!\n";

In reply to Re: Number Guess by Tanktalus
in thread Number Guess by cryptoquip

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • 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:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.