Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris
 
PerlMonks  

Re: Repeat question

by davido (Cardinal)
on Nov 30, 2018 at 17:09 UTC ( #1226542=note: print w/replies, xml ) Need Help??


in reply to Repeat question

IO::Prompt::Hooked based solution:

while (1) { my @random_int = sort {$b <=> $a} map {int(rand(100))} 0..1; my $correct = 0; last if !defined prompt( message => "What is $random_int[0] - $random_int[1]? ('q' +to exit) ", validate => sub { my $raw = shift; return 0 unless looks_like_number($raw); return $correct = $raw == $random_int[0] - $random_int[1]; }, error => sub { my $raw = shift; return looks_like_number($raw) ? "Sorry, you are wrong. Have another go.\n" : "You need to enter a number.\n"; }, escape => qr/^q$/i, ); print "Well done! You are correct!\n\n" if $correct; }

It's not a great solution, but it's pretty easy to work with.

It might be worth explaining: The call to prompt only happens one time per random number, and the validation hook will cause prompt to re-query automatically until the validation hook passes.


Dave

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1226542]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others avoiding work at the Monastery: (6)
As of 2019-05-20 19:43 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    Do you enjoy 3D movies?



    Results (128 votes). Check out past polls.

    Notices?