1
$\begingroup$

The function Input[] when evaluated displays a dialog box where one can enter any character. My question is how do I restrict the input only to a certain type such as numbers or string? I know there is InputString that takes in only strings.

This is what I have tried:

x = Input["Enter a number"];
While[Head[x] == Symbol, x = Input["Enter a number"]]

I believe there is a better solution than this. Can anyone help?

Edit

I am aware of the InputField function. However, I am trying to implement something with the Wolfram Engine and that does not have a front end and I will be using it from the command prompt. Hence, I need to know a way to restrict the input type for the function Input.

$\endgroup$
2
  • $\begingroup$ Then just check the result, if it's not what you want, prompt again. $\endgroup$
    – Szabolcs
    Oct 31, 2019 at 10:28
  • $\begingroup$ For the WE I'd go with your approach and upgrade it a little: While[Not@MatchQ[x, _String | _?NumericQ], x = Input["Enter a number or a string"]] $\endgroup$
    – Kuba
    Oct 31, 2019 at 10:30

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Browse other questions tagged or ask your own question.