in reply to Re^2: Repeat question (redo)
in thread Repeat question
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.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^4: Repeat question (redo)
by hchana (Acolyte) on Nov 30, 2018 at 14:28 UTC | |
by hippo (Archbishop) on Nov 30, 2018 at 15:18 UTC | |
by hchana (Acolyte) on Nov 30, 2018 at 15:38 UTC | |
by hchana (Acolyte) on Dec 11, 2018 at 11:27 UTC | |
by hippo (Archbishop) on Dec 11, 2018 at 12:05 UTC |