Course of Raku / Essentials / Built-in functions for printing / Exercises / Print the warning

Solution: Print the warning

Code

Here is the solution:

my $age = prompt 'What is your age? ';

if $age < 0 {
    note 'You entered a negative number!';
}
else {
    say "Your age is $age";
}

🦋 Find the program in the file print-the-warning.raku.

Output

A couple of possible cases:

$ raku exercises/built-in-functions-for-printing/print-the-warning.raku 
What is your age? 20
Your age is 20

$ raku exercises/built-in-functions-for-printing/print-the-warning.raku 
What is your age? -1
You entered a negative number!

To make sure the error message is printed to STDERR, redirect the output. The prompt to enter the age will not appear, but you can still enter a number. The warning remains visible.

$ raku exercises/built-in-functions-for-printing/print-the-warning.raku > /dev/null
20

$ raku exercises/built-in-functions-for-printing/print-the-warning.raku > /dev/null 
-1
You entered a negative number!

Course navigation

More about functions / Multi-functions   |   The MAIN function