in reply to Repeat question
$type hchana.pl use strict; use warnings; use IO::Prompt::Hooked; srand(999); # Delete for production my $correct_ans; my %opt = ( escape => qr/q(uit)?/i, # Type 'quit' to exit program error => "Sorry you're wrong, have another go.\n\n", validate => sub { $_[0] !~ /\D/ and $_[0] == $correct_ans}, ); while (1) { my $random_integer1=int(rand(100)); my $random_integer2=int(rand(100)); my $sign = ($random_integer1 > $random_integer2) ? '-' : '+'; $correct_ans = $random_integer1 + (($sign eq '+') ? $random_integer2 : -$random_integer2); my $msg = "What is $random_integer1 $sign $random_integer2?"; my $ans = prompt( message => $msg, %opt ); last if !defined $ans; print "Well done.! $ans is correct.\n\n"; } warn "Program terminated by user\n"; $perl hchana.pl What is 10 + 63? 73 Well done.! 73 is correct. What is 80 + 92? too hard Sorry you're wrong, have another go. What is 80 + 92? 22 Sorry you're wrong, have another go. What is 80 + 92? 172 Well done.! 172 is correct. What is 77 - 53? 44 Sorry you're wrong, have another go. What is 77 - 53? 24 Well done.! 24 is correct. What is 59 - 41? quit Program terminated by user $
UPDATE: Started this yesterday. Did not notice that Dave had posted a similar solution while I was sleeping. Note that this version does handle sign the same as the OP.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: Repeat question
by davido (Cardinal) on Dec 02, 2018 at 02:42 UTC |