Professional Web Applications Themes

guessing script - PERL Beginners

Learning Perl here.. So I wrote this number guessing script but can't see what is wrong with it. I can never get the solution even when I know it's correct. What am I doing wrong? #!/usr/bin/env perl use Math::Random; use strict; sub run(){ my $solution = random_uniform_integer(1, 1, 100); my $guess = undef; do { print "\$solution = $solution\n"; print "Guess (1 - 100) ?\n"; chomp(my $guess = <STDIN>); if ($guess > $solution) { print "$guess is too HIGH!\n"; } if ($guess < $solution) { print "$guess is too LOW!\n"; } } while ($guess != $solution); print "$guess is correct, ...

  1. #1

    Default guessing script


    Learning Perl here.. So I wrote this number guessing script but can't see
    what is wrong with it. I can never get the solution even when I know it's
    correct. What am I doing wrong?

    #!/usr/bin/env perl

    use Math::Random;
    use strict;

    sub run(){
    my $solution = random_uniform_integer(1, 1, 100);
    my $guess = undef;
    do {
    print "\$solution = $solution\n";
    print "Guess (1 - 100) ?\n";
    chomp(my $guess = <STDIN>);
    if ($guess > $solution) {
    print "$guess is too HIGH!\n";
    }
    if ($guess < $solution) {
    print "$guess is too LOW!\n";
    }
    } while ($guess != $solution);
    print "$guess is correct, YAY!\n";
    print "Play again? ";
    chomp(my $answer = <STDIN>);
    if (lc($answer) eq 'y') {
    run();
    }
    }

    run();


    TIA :)

    --
    Greg Donald
    com
    Greg Guest

  2. #2

    Default RE: guessing script

    (my $guess = <STDIN>); 'my' is your problem.. that creates new varialbe remove it..
    HTH
    Jay

    -----Original Message-----
    From: Greg Donald [mailto:com]
    Sent: Thursday, April 15, 2004 11:41 AM
    To: org
    Subject: guessing script



    Learning Perl here.. So I wrote this number guessing script but can't see
    what is wrong with it. I can never get the solution even when I know it's
    correct. What am I doing wrong?

    #!/usr/bin/env perl

    use Math::Random;
    use strict;

    sub run(){
    my $solution = random_uniform_integer(1, 1, 100);
    my $guess = undef;
    do {
    print "\$solution = $solution\n";
    print "Guess (1 - 100) ?\n";
    chomp(my $guess = <STDIN>);
    if ($guess > $solution) {
    print "$guess is too HIGH!\n";
    }
    if ($guess < $solution) {
    print "$guess is too LOW!\n";
    }
    } while ($guess != $solution);
    print "$guess is correct, YAY!\n";
    print "Play again? ";
    chomp(my $answer = <STDIN>);
    if (lc($answer) eq 'y') {
    run();
    }
    }

    run();


    TIA :)

    --
    Greg Donald
    com

    --
    To unsubscribe, e-mail: org
    For additional commands, e-mail: org
    <http://learn.perl.org/> <http://learn.perl.org/first-response>


    Jayakumar Guest

  3. #3

    Default Re: guessing script

    In a message dated 4/15/2004 12:09:40 PM Eastern Daylight Time,
    com writes:
    Learning Perl here.. So I wrote this number guessing script but can't see
    what is wrong with it. I can never get the solution even when I know it's
    correct. What am I doing wrong?
    [Snipped Code]

    It has to do with the scope of guess. You used my twice. Get rid of that my
    in "chomp(my $guess = <STDIN>); " and then it's good. The $guess being
    evaluated in the while() condition is undef because that is what it has been defined
    to be in that scope. By saying "chomp(my $guess = <STDIN>);" you're saying
    that this is kind of a new $guess specific to that block, and it doesn't even
    exist outside of it.

    -will
    (the above message is double rot13 encoded for security reasons)

    Most Useful Perl Modules
    -strict
    -warnings
    -Devel::DProf
    -Benchmark
    -B::Dep
    -Data::Dumper
    -Clone (a Godsend)
    -Perl::Tidy
    -Beautifier

    WilliamGunther@aol.com Guest

  4. #4

    Default Re: guessing script

    On Apr 15, 2004, at 10:40 AM, Greg Donald wrote:
     

    Looks like you already got some good answers. Let me add a few minor
    comments...
     

    You should add a:

    use warnings;

    I believe this would have even caught your previous mistake.
     

    Don't declare subroutines like that. Use:

    sub run { # no parens
     

    The above should probably be:

    elsif ($guess < $solution) {
     

    Move the rest of this subroutine outside, into the general script
    (around your run(); call). There's no need to use recursion for this.

    James
     

    James Guest

  5. #5

    Default Re: guessing script

    On Thursday 15 April 2004 12:43 pm, James Edward Gray II wrote: 

    Thanks for you comments. Is this the best way then? Seems to work, but I'm
    curious what could be any better..

    #!/usr/bin/env perl

    use Math::Random;
    use warnings;
    use strict;

    my $answer = undef;

    sub run {
    my $solution = random_uniform_integer(1, 1, 100);
    my $guess = undef;
    my $tries = 0;
    do {
    print "Guess (1-100)?\n";
    chomp($guess = <STDIN>);
    $tries++;
    if ($guess > $solution) {
    print "$guess is too HIGH!\n";
    } elsif ($guess < $solution) {
    print "$guess is too LOW!\n";
    }
    } while ($guess != $solution);
    my $try_word = $tries == 1 ? "try" : "tries";
    print "$guess is correct, YAY! It only took you $tries $try_word!\n";
    }

    do {
    run();
    print "Play again? ";
    chomp($answer = <STDIN>);
    } while (lc($answer) eq 'y');

    --
    Greg Donald
    com
    Greg Guest

  6. #6

    Default Re: guessing script

    On Apr 15, 2004, at 1:24 PM, Greg Donald wrote:
     
    >
    > Thanks for you comments. Is this the best way then? Seems to work,
    > but I'm
    > curious what could be any better..[/ref]

    I think those changes are for the better, yes.

    James
     

    James Guest

Similar Threads

  1. ksh script - Preventing multiple runs of a particular script
    By Melissa in forum Linux / Unix Administration
    Replies: 4
    Last Post: June 26th, 01:49 PM
  2. 6.1 script not working on 7.0 server. Script used towork!
    By piaposa in forum Coldfusion Flash Integration
    Replies: 1
    Last Post: February 9th, 03:43 PM
  3. Secure Login Database from people guessing filename.
    By Malic in forum Macromedia Dreamweaver
    Replies: 1
    Last Post: July 12th, 08:38 AM
  4. Execute shell script from a perl script
    By kderaedt in forum Perl / CGI
    Replies: 3
    Last Post: June 26th, 08:26 PM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139