Skip to content

Commit

Permalink
arithmetic: empty answer is not a number (#541)
Browse files Browse the repository at this point in the history
* arithmetic: empty answer is not a number

* regex m/\D/ didn't match on empty string, so previously if I typed enter instead of a number it was treated as a wrong answer
* It's more correct to treat empty line as bad input and prompt again for a number
* While here, make the input loop more user friendly by trimming leading and trailing spaces before testing for m/\D/

* back out 'use warnings'
  • Loading branch information
mknos committed Apr 10, 2024
1 parent b9e7df6 commit 84d9a5b
Showing 1 changed file with 3 additions and 1 deletion.
4 changes: 3 additions & 1 deletion bin/arithmetic
Expand Up @@ -100,7 +100,9 @@ while ($questions < QUESTIONS) {
report();
}
chomp $guess;
if ($guess =~ /\D/) {
$guess =~ s/\A\s+//;
$guess =~ s/\s+\Z//;
if (length($guess) == 0 || $guess =~ /\D/) {
print "Please type a number.\n";
redo;
}
Expand Down

0 comments on commit 84d9a5b

Please sign in to comment.