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.
Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
Read Where should I post X? if you're not absolutely sure you're posting in the right place.
Please read these before you post! —
Posts may use any of the Perl Monks Approved HTML tags:
- a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
| |
For: |
|
Use: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.