in reply to Repeat question

TIMTOWTDI:

(as a variation of Haukex' answer, which is more straight forward )

>perl my $answer = 42; { print "What is the answer? "; chomp( my $input = <STDIN> ); if ( $input == $answer ) { print "Good!\n"; } else { print "Please try again.\n"; redo; } } __END__ What is the answer? 1 Please try again. What is the answer? 2 Please try again. What is the answer? 42 Good!

Cheers Rolf
(addicted to the Perl Programming Language :)
Wikisyntax for the Monastery FootballPerl is like chess, only without the dice

Replies are listed 'Best First'.
Re^2: Repeat question (redo)
by hchana (Acolyte) on Nov 30, 2018 at 13:56 UTC

    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.?

      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;
      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.