First the code:
print "Question #1: Who is buried in Grant's tomb?\n\n"; print "A. Grant\n"; print "B. Yoda\n"; print "C. Liberace\n"; print "D. None of the above.\n\n\n\n"; print "The answer is: "; chomp($reply = <STDIN>); #Following loop to test for a-d answers, courtesy of PerlMonks.org. #This works but will not accept any answer without going thru this #code block once print "\nPick a letter between a and d.\n"; chomp(my $reply = <STDIN>); while ($reply !~ /^[a-d]$/i) { print "\nPick a letter between a and d -- or else!\n"; chomp($reply = <STDIN>); }
If you've read the comments, you know what I can't fix. The code that I got here at Perlmonks does the job, but no response to the question will be accepted until that "while" loop runs once. When I asked for help a couple of weeks ago, I didn't include the code that showed the question. Maybe that would have made a difference. So, the question now is: How can I make sure that the answer to the question shown in the code is in the range a-d without showing the warning "Pick a letter between a and d" when the answer falls in the proper range?
In reply to while loop almost correct by irvson
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |