Hi!
I have the following code which I wrote for a guessing number script. The purpose of this code was to make a program that guesses a number between 1 and 10 that the user thinks of. It should make a guess, and the user shall answer yes (if correctly guessed), higher (if the number the user thought of was higher than the quess) or lower (if the number the user thought of was lower than the quess). The program ends when the number is guessed correctly, otherwise it tries again.
print "Think of a number between 1 and 10\n"; $user_answer = <STDIN>; $program_guess = 0; while ($user_answer ne 'YES') { $program_guess++; print "Is the number you thought of $program_guess ? Answer yes, hi +gher or lower\n"; $answer = <STDIN>; chomp $user_answer; } print "Program finished. The number you guessed was $program_guess.\n" +;

Now, the goal is to make it more advanced:
I need to count how many guesses it took for the program until it finds the correct number (easy one, just a counter there) but also to make the program guess the number in the fewest guesses possible...How can I do that?

In reply to Updates to a guessing number script by Anonymous Monk

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.