in reply to Re: Repeat question (redo)
in thread Repeat question

this seems to be more of a guessing game, where the number is pre-defined. I wish to generate 2 random integers and create a sum e.g. '23-16=' whereby the larger integer is identified and placed on the left hand side. Only when the user inputs the right answer will a new sum be generated with two new integers. From my original code, a new sum is generated even if you get the answer wrong, how do I get the code to repeat the question answered incorrectly.? Do I need to store the random integers in variables or something.?

Replies are listed 'Best First'.
Re^3: Repeat question (redo)
by pryrt (Abbot) on Nov 30, 2018 at 14:08 UTC

    You would put one of the constructs suggested by haukex or LanX inside the while from your original code, around your if/else block.

    (Also, rather than having that if/else block, and repeating two almost-identical pieces of code: after generating the two random numbers, I would suggest using this to swap them, so the first is always bigger than the second:

    if($random_integer1 < $random_integer2) { my $tmp = $random_integer2; $random_integer2 = $random_integer1; $random_integer1 = $tmp; }
    That way, you only need one piece of code for asking and checking the answer, rather than two nearly-identical pieces. There are more perlish ways of doing the swap, but I chose to make it easy for you to understand.)

      This is perfectly OK, but the temporary variable is not really needed and this can be simplified as follows:
      ($random_integer1, $random_integer2) = ($random_integer2, $random_inte +ger1) if $random_integer1 < $random_integer2;

        that was exactly one the "more perlish ways" I was referencing. I would have done it that way for myself, but I thought the explicit swap was more concrete for the OP to understand what was going on.

Re^3: Repeat question (redo)
by hippo (Archbishop) on Nov 30, 2018 at 14:08 UTC
    Do I need to store the random integers in variables or something.?

    Yes, but you are already doing that. Here's one approach with a subroutine and using an array to hold the operands.

    #!/usr/bin/env perl use strict; use warnings; while (1) { my @operands = (int(rand(100)), int(rand(100))); ask ($operands[0] > $operands[1], @operands); } sub ask { my ($subtract, @args) = @_; my $input; my $answer = $subtract ? $args[0] - $args[1] : $args[0] + $args[1] +; my $oper = $subtract ? '-' : '+'; until (defined $input && $input == $answer) { print "Nope, try again\n" if defined $input; print "What is $args[0] $oper $args[1] ? "; chomp( $input = <STDIN> ); }; print "Success!\n"; }

    Edit: sigil fix on the ask() call.

      wow.! this works great. Thank you. However, it appears rather complicated for me to understand and therefore learn from. Can I be cheeky and ask what the code is doing line per line.? No worries if you can't.

        Well, I don't want to over-explain since the best way to learn is often to figure things out for yourself. However, let's examine what I suppose could be the 2 most confusing parts.

        ask ($operands[0] > $operands[1], @operands);

        This is a subroutine call. You'll see we've defined the subroutine ask in the script? This is how it is called. There are either 2 or 3 arguments inside the brackets depending on how you see things. The first one is $operands[0] > $operands[1] which is a logical expression and therefore either true or false and the second one is @operands which is an array with the 2 random values previously assigned. Note that originally this line had an @ symbol at the start of $operands[0] which although OK was misleading (and a typo) so I've corrected it now.

        my $answer = $subtract ? $args[0] - $args[1] : $args[0] + $args[1];

        This is a variable assignment which computes the answer in one way or another depending on the logical value in $subtract which we passed into the sub as the first argument. It uses the ternary operator which is of the form condition ? eval-if-true : eval-if-false. Think of it as a short-hand way of writing an if-else block.

        The rest of the code is just loops and prints, really. See if you can trace through the logic now.