dialog.ask command doesn’t restrict or verify the user input and it always stores the input data as text. If you want to limit the input only to number values, for example, and perform calculations on this input, you have to add some more operations.
As you remember from one of the previous lessons, the variables can be of different types. A value entered in the
dialog.ask dialog box is text, so you can’t use it for mathematical operations. But it’s easy to transform a text variable into a numeric one — float or integer — using casting or forcing type:
dialog.ask ‴Enter the first digit (0-9):‴ result ♥input1 dialog.ask ‴Enter the second digit (0-9):‴ result ♥input2 ♥input1 = ⟦integer⟧♥input1 ♥input2 = ⟦integer⟧♥input2 dialog ♥input1+♥input2
First two lines ask a user for two digits, which are stored in the
♥input1 and the
♥input2 variables as text by default. Then both variables are transformed into integers, so it’s possible to add them and display the result in a dialog box.
But what happens when you enter other characters than digits? Yes, an error occurs: the robot can’t transform such values into integers.
In the next lesson you will see how you can leverage errors and use them in your favor.