in reply to Re: Regarding Declaring Variables
in thread Regarding Declaring Variables

Just a small alteration changing the hard coded 12 to something random:
use strict; my $target = sprintf "%.$_[1]f", rand(10); print "Guess my number.\n"; while ((print "Enter your guess: "), my $guess = <STDIN>) { (print "You guessed it!"), last if $guess == $target; (print "You guessed too low\n"), next if $guess < $target; (print "You guessed too high\n"), next if $guess > $target; }

Replies are listed 'Best First'.
Re^3: Regarding Declaring Variables
by GrandFather (Saint) on Nov 17, 2005 at 19:40 UTC
    my $target = 1 + int rand (20); # number 1 - 20

    would be more conventional for generating a random integer. Note that your version generates a number in the range 0 - 9 which may not be what some people would expect.


    DWIM is Perl's answer to Gödel
      No, %f rounds, so it generates a number in the range 0-10, but 0 and 10 have only half the chance of occuring that other numbers do.