Help with Incrementing

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • TXpugslave
    New Member
    • Feb 2008
    • 4

    Help with Incrementing

    And here it is.
    I'm just, JUST learning how to use perl, and how to do any sort of programming in general. I'm trying to self-teach and have read a book on C programming and wanted to try PERL. I'm really very green at this and don't want to give up, but its not always easy to move forward without help so here goes. I have this simple, simple program that I am trying to write based off of some other examples I have read. Unfortunately, in order to get it to run in five or less trys, I do not think I'm executing the incrementing correctly. Can anyon take a quick look, point, laugh, and maybe guide me toward the sun?
    So much appreciated, thank you.

    [CODE=perl]#!/user/bin/perl

    my $count = 0;
    my $num = int(rand (21));
    my $guess = <STDIN>;

    do {
    print "Guess a number from 1-20:";

    if($guess == $num) {
    print "You win. Go start your 1-900 number

    immediately.\n" ;
    }
    elsif( $guess > 20) {
    print "Uhh, lets try that again. A number,

    one through 20. Go:\n";
    }
    elsif( $guess < 1) {
    print "No dice. 1-20. Try again.\n";
    }
    $count++;


    while ($guess!= $num) {
    if ($count <=4) {
    print "Nope. Guess again.\n";
    }
    else ($count == 5) {
    print "You lose. Don't quit your day job.\n" || die;
    }[/CODE]
    Last edited by eWish; Feb 29 '08, 12:37 AM. Reason: Please use [CODE][/CODE] tags
  • eWish
    Recognized Expert Contributor
    • Jul 2007
    • 973

    #2
    We like it when people are trying to learn. Therefore, we are not going to laugh at you. Try removing the while loop and see if that does what you want.

    Change:
    [CODE=perl]while ($guess!= $num) {
    if ($count <=4) {
    print "Nope. Guess again.\n";
    }
    else ($count == 5) {
    print "You lose. Don't quit your day job.\n" || die;
    }
    [/CODE]
    To:
    [CODE=perl]if ($count <=4) {
    print "Nope. Guess again.\n";
    }
    else ($count == 5) {
    print "You lose. Don't quit your day job.\n" || die };[/CODE]
    Also, look into using the following pragmas. They will save you time debugging and help you write cleaner & stricter code.

    strict - Perl pragma to restrict unsafe constructs
    warnings - Perl pragma to control optional warnings

    --Kevin

    Comment

    • TXpugslave
      New Member
      • Feb 2008
      • 4

      #3
      Thank you so much for your help. I have tried something simaliar to that... and even replacing it with the exact code you provided, I get the following error in the command prompt window:

      Syntax error at... line 26 near "else ("
      Missing right curly or square bracket at line 28, at end of line

      Then it just says execution aborted due to compilation errors.

      I think overall, it feels like a messy program with so many if statements, but like I said, I'm just starting. Here again is the code that I have including the revisions you suggested.


      [CODE=perl]#!/user/bin/perl

      my $count = 0;
      my $num = int(rand (21));
      my $guess = <STDIN>;

      do {
      print "Guess a number from 1-20:";

      if($guess == $num) {
      print "You win. Go start your 1-900 number

      immediately.\n" ;
      }
      elsif( $guess > 20) {
      print "Uhh, lets try that again. A number,

      one through 20. Go:\n";
      }
      elsif( $guess < 1) {
      print "No dice. 1-20. Try again.\n";
      $count++;

      }


      if ($count <=4) {
      print "Nope. Guess again. \n";
      }
      else ($count == 5) {
      print "You lose. Don't quit your day job.\n" ||

      die};[/CODE]

      ----- Thanks again!
      Last edited by eWish; Feb 29 '08, 01:37 AM. Reason: Please use [CODE][/CODE] tags

      Comment

      • eWish
        Recognized Expert Contributor
        • Jul 2007
        • 973

        #4
        When posting code here please use the &#91;CODE=pe rl]&#91;/CODE] tags.

        The you have forgot the closing bracket on the do{} block of code.

        --Kevin

        Comment

        • KevinADC
          Recognized Expert Specialist
          • Jan 2007
          • 4092

          #5
          The first thing that jumps out is this:

          Code:
           else ($count == 5) {
          an "else" block does not have a conditional expression (xxxxx). This error should prevent your program from running, the correct syntax is:

          Code:
          if (condition) {
             expression
          }
          elsif (condition) { # optional elsif block, there can be more
             expression
          {
          else {# optional else block but there can only be one
             fall through condition
          }
          "elsif" and "else" are both optional after an "if" block. But "else" has no condition like "if" and "elsif".

          There might be other problems with your code too, I will check later.

          Comment

          • TXpugslave
            New Member
            • Feb 2008
            • 4

            #6
            Thank you so much. I think my problems run much deeper than I thought.

            Comment

            • mehj123
              New Member
              • Aug 2007
              • 55

              #7
              Hi....
              The do loop you are using should have a condition to determine where the loop should stop its execution. And [CODE=perl] $guess = <STDIN>; [/CODE] should be inside the loop only.. One more change that is required is for each time the user fails to guess the number correctly you should increase the $count... Moreover, as mentioned by KevinADC, else construct does not have any conditions .With these changes the code is given below.. it would be needed to be refined furthur.. :)

              [CODE=perl]#/usr/bin/perl
              use strict;
              use warnings;
              my $count = 0;
              my $num = int(rand (21));
              my $guess = 0;

              do
              {
              print "Guess a number from 1-20:";
              $guess = <STDIN>;
              if($guess == $num)
              {
              print "You win. Go start your 1-900 number immediately.\n" ;
              exit(0);
              }
              elsif( $guess > 20)
              {
              print "Uhh, lets try that again. A number,one through 20. Go:\n";
              }
              elsif( $guess < 1)
              {
              print "No dice. 1-20. Try again.\n";
              }
              if ($count <=4)
              {
              print "Nope. Guess again. \n";
              $count++;
              }
              else
              {
              print "You lose. Don't quit your day job.\n";
              exit(0);
              }
              }while($count<= 5);[/CODE]

              Hope this helps..
              Mehj

              Comment

              • TXpugslave
                New Member
                • Feb 2008
                • 4

                #8
                very helpful, thank you. I see just what you are saying!

                Comment

                • KevinADC
                  Recognized Expert Specialist
                  • Jan 2007
                  • 4092

                  #9
                  May as well clean it all up:


                  [CODE=perl]#!/usr/bin/perl
                  use strict;
                  use warnings;
                  my $count = 5;
                  my $num = int(rand (21));
                  print "Guess a number from 1-20:";
                  chomp(my $guess = <STDIN>);

                  do {
                  if( $guess > 20 || $guess < 1) {
                  print "Uhh, lets try that again. Enter a number, 1 through 20:";
                  chomp($guess = <STDIN>);
                  }
                  elsif (--$count == 0) {
                  print "You lose. The number was $num.\n";
                  }
                  elsif( $guess != $num) {
                  print "Wrong guess. $count more tries left. Enter a number, 1 through 20:";
                  chomp($guess = <STDIN>);
                  }
                  else {
                  print "You win. $num is correct! Go start your 1-900 number immediately.\n" ;
                  $count=0;
                  }
                  } while( $count );[/CODE]

                  Comment

                  Working...