Message Boards Message Boards

0
|
2844 Views
|
2 Replies
|
0 Total Likes
View groups...
Share
Share this post:

How to correctly use While with Ask and AskFunction?

Posted 2 years ago

Dear all, I am trying to use AskFunction to perform the following actions (building a list of names):

  1. Ask the user to enter a name (string)
  2. Ask if the user wants to enter an additional name (or not)
  3. If the answer to 2. is yes, then ask to enter a new name. If the answer to 2. is no, then it's done
  4. While[2., 3] (ask if the user wants to enter an additional name, if "yes" ask to enter the name, if "no" exit the while loop.

I've tried the following (without success) :

f = AskFunction[
   Ask[{"name", "Enter a name"} -> "String"];
   Ask[{"answer", 
      "Do you want to add another name ?"} -> {"Yes" -> True, 
      "No" -> False}];
   While[
    Ask[{"answer", 
       "Do you want to add another unit name ?"} -> {"Yes" -> True, 
       "No" -> False}],
    AskAppend["name"];
    ];
   Ask["name"]
   ];

f[]
POSTED BY: Mario Veruete
2 Replies
Posted 2 years ago

Hi ! I know now how to do it :

fWhile = AskFunction[
   Ask[{"name", "Enter a name"} -> "String"];
   Ask[{"anotherQ", "Do you want another?"} -> {"Yes" -> True, 
      "No" -> False}];
   If[
    AskedValue["anotherQ"],
    AskAppend["name"]
    ];
   AskAppend["anotherQ"];
   While[
    Last[AskState[]["anotherQ"]],
    AskAppend["name"];
    AskAppend["anotherQ"]
    ];
   Ask["name"]
   ];


While[]

Best

POSTED BY: Mario Veruete
Posted 2 years ago

From the documentation for Ask:

The first time Ask["key"] occurs in the evaluation of AskFunction[{[Ellipsis],"key"->spec,[Ellipsis]},body], a value associated with "key" is interactively requested using the form and interpretation specified by spec. Subsequent evaluations of Ask["key"] return the stored value.

I haven't used the various Ask* functions, and I notice that they are labeled as experimental, so I don't have any advice on using them. However, I have used FormFunction and related functions. You can use them to create fairly complex input forms for getting data from a user. Here is a quick example from the documentation that shows how to use a sort of expandable input widget to get a list of values:

FormFunction["field" -> RepeatingElement["String"]][]

These functions are typically deployed (with CloudDeploy) as a web resource. If you're just wanting interaction in a local notebook, you could look at Input, InputString, or similar functions.

POSTED BY: Eric Rimbey
Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
Attachments
Remove
or Discard

Group Abstract Group Abstract