Simple if/else problem.

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • gloomer
    New Member
    • Apr 2007
    • 13

    Simple if/else problem.

    I'm trying to make a really really really basic rpg. This is how it starts.

    Unfortunately, I seem to have gotten my if/elsif statements mixed up. Because whatever option you choose, it always prints 'LOLOL'

    I'm not sure why.

    [code=perl]

    #!/usr/bin/perl

    $left = 'left';
    $right = 'right';

    print "You\'re walking along, and you see a fork in the road. What do you do\?.\n";

    print "Turn left\?\n";

    print "Turn right\?\n";

    $choice = <stdin>;
    chomp $choice;

    if ($choice == $left) {
    print "LOLOL";
    }
    elsif ($choice == $right) {
    print "HAHA";
    }

    else {
    print "PRINT A VALID OPTION MAAAAAN";
    }

    [/code]
    Last edited by numberwhun; Sep 6 '07, 05:22 PM. Reason: add code tags
  • numberwhun
    Recognized Expert Moderator Specialist
    • May 2007
    • 3467

    #2
    Originally posted by gloomer
    I'm trying to make a really really really basic rpg. This is how it starts.

    Unfortunately, I seem to have gotten my if/elsif statements mixed up. Because whatever option you choose, it always prints 'LOLOL'

    I'm not sure why.
    Well, your script has a few issues with it that you needed to correct. First, you should always "use warnings;" and "use strict;". Doing so would have shown you a slew of errors because you didn't define your variables with 'my'. After correcting that, you would have received the error that you were doing a numeric comparison (==) when you should be doing a string comparisong (eq). This is something you need to be careful of.

    Plus, when coding, be sure that if you are wanting your users to make a choice, let them know what valid options are. If your users type in "Turn left", the script will not work, even though that is what you asked them for.

    Here is a version of your script that works, with some improvements. I also think that you should take a read of the perlop page on perldoc. It will explain the difference(s) between == and eq and many others.

    [code=perl]
    #!/usr/bin/perl

    use strict;
    use warnings;

    my $left = 'left';
    my $right = 'right';

    print "You\'re walking along, and you see a fork in the road. What do you do\?.\n";

    print "To Turn Left(type left)\?\n";

    print "To Turn Right(type right)\?\n";

    my $choice = <STDIN>;
    chomp $choice;

    if ($choice eq $left) {
    print "LOLOL";
    }
    elsif ($choice eq $right) {
    print "HAHA";
    }

    else {
    print "PRINT A VALID OPTION MAAAAAN";
    }
    [/code]

    Comment

    • Alan Bak
      New Member
      • Dec 2006
      • 12

      #3
      Originally posted by gloomer
      I'm trying to make a really really really basic rpg. This is how it starts.

      Unfortunately, I seem to have gotten my if/elsif statements mixed up. Because whatever option you choose, it always prints 'LOLOL'

      I'm not sure why.
      I think that the if loop is fine but there is a problem with the variable $left and $right being strings and the test being == rather than eq. I think that this works although my reasoning may be incorrect.

      cheers

      [code=perl]
      #!/usr/bin/perl

      $left = "left";
      $right = "right";

      print "You\'re walking along, and you see a fork in the road. What do you do\?.\n";

      print "Turn left\?\n";

      print "Turn right\?\n";

      $choice = <stdin>;
      chomp $choice;

      if ($choice eq $left) {
      print "LOLOL";
      }
      elsif ($choice eq $right) {
      print "HAHA";
      }
      else {
      print "PRINT A VALID OPTION MAAAAAN";
      }
      [/code]
      Last edited by numberwhun; Sep 6 '07, 05:30 PM. Reason: add code tags, fix formatting (bring quoted text to top)

      Comment

      • numberwhun
        Recognized Expert Moderator Specialist
        • May 2007
        • 3467

        #4
        Originally posted by Alan Bak
        I think that the if loop is fine but there is a problem with the variable $left and $right being strings and the test being == rather than eq. I think that this works although my reasoning may be incorrect.

        cheers

        [code=perl]
        #!/usr/bin/perl

        $left = "left";
        $right = "right";

        print "You\'re walking along, and you see a fork in the road. What do you do\?.\n";

        print "Turn left\?\n";

        print "Turn right\?\n";

        $choice = <stdin>;
        chomp $choice;

        if ($choice eq $left) {
        print "LOLOL";
        }
        elsif ($choice eq $right) {
        print "HAHA";
        }
        else {
        print "PRINT A VALID OPTION MAAAAAN";
        }
        [/code]
        First, if you are going to step on my post, then please be kind enough to include code tags around your code.

        Second, please, DO NOT top post. It is annoying to everyone who reads your posting because.....

        say.
        to
        trying
        are
        you
        what
        tell
        to
        able
        be
        won't
        they

        I have fixed both of these issues in the post that you provided.

        Regards,

        Jeff

        Comment

        • Alan Bak
          New Member
          • Dec 2006
          • 12

          #5
          Sorry about that Jeff. This was the first question that I have come across on the forum that I could actually be of some help on! When I started to answer it there were no replies and after I posted I saw yours.


          Cheers

          Alan

          Comment

          • numberwhun
            Recognized Expert Moderator Specialist
            • May 2007
            • 3467

            #6
            No worries. :-)

            Regards,

            Jeff

            Comment

            • gloomer
              New Member
              • Apr 2007
              • 13

              #7
              Why the heck do you need to put 'my' in front of all the variables?

              You can't just define variables?

              Comment

              • numberwhun
                Recognized Expert Moderator Specialist
                • May 2007
                • 3467

                #8
                Originally posted by gloomer
                Why the heck do you need to put 'my' in front of all the variables?

                You can't just define variables?
                Sure, you can just define variables, if you don't "use strict;". The strict pragma forces you to define your variables with my before or when they are first used. This will further explain the strict pragma for you.

                Regards,

                Jeff

                Comment

                Working...