       Re: Buttons to interactively restart/finish a program

• To: mathgroup at smc.vnet.net
• Subject: [mg99230] Re: Buttons to interactively restart/finish a program
• From: "Sjoerd C. de Vries" <sjoerd.c.devries at gmail.com>
• Date: Thu, 30 Apr 2009 06:24:05 -0400 (EDT)
• References: <gt3tml\$nps\$1@smc.vnet.net>

```Hi Alexei,

A very quick change in your code makes the restart button work: add
the quiz part of the panel and it works (see below).

Note that you assume that RandomInteger yields a zero when the upper
boundary of the range is lower than the lower boundary. This is not
true. Try, for instance, this: RandomInteger[{1, 0}, 100]. It is
possible that you get a 4+1 question for which there is no answer
button, so you should add one.

Cheers -- Sjoerd

Panel[
DynamicModule[{a, b},

a = RandomInteger[{1, 4}];
b = RandomInteger[{1, 4 - a}];
f = "?";
g1 := (Clear[f]; f = a + b);
g2 := (Clear[f]; f = "?");

Column[{Row[{Dynamic@Button[Style[1, 30], If[a + b == 1, g1, g2]],
Dynamic@Button[Style[2, 30], If[a + b == 2, g1, g2]],
Dynamic@Button[Style[3, 30], If[a + b == 3, g1, g2]],
Dynamic@Button[Style[4, 30], If[a + b == 4, g1, g2]]}],
Dynamic@Panel[
Row[{Text[Style[a, 50]], Text[Style["+", 50]],
Text[Style[b, 50]], Text[Style["=", 50]],
Text[Style[Dynamic[f], 50]]}]],
Button["Restart", a = RandomInteger[{1, 4}];
b = RandomInteger[{1, 4 - a}];
f = "?";
g1 := (Clear[f]; f = a + b);
g2 := (Clear[f]; f = "?");]}]]]

On Apr 27, 11:25 am, Alexei Boulbitch <Alexei.Boulbi... at iee.lu> wrote:
> Dear community,
> I am writing a simple interactive teaching program to teach my child
> arithmetics.  Here is a simplified example on addition from 1 to 4:
>
> Panel[DynamicModule[{a, b},
>               Clear[a, b, f];
>                   a = RandomInteger[{1, 4}];
>                                 b = Ran=
domInteger[{1, 4 - a}];
>                                     =
f = "?";
>   g1 := (Clear[f]; f = a + b);
>   g2 := (Clear[f]; f = "?");
>
>   Column[{
>             Row[{
>
>               Dynamic@Button[Style[1, 30], If[a + b == =
1, g1, g2]],
>                   Dynamic@Button[Style[2, 30], If[a + b=
== 2, g1, g2]],
>                       Dynamic@Button[Style[3, 30], =
If[a + b == 3, g1, g2]],
>                           Dynamic@Button[Style[=
4, 30], If[a + b == 4,
> g1, g2]]
>
>                             }],
>
>                 Panel[Row[{Text[Style[a, 50]],
>                       Text[Style["+", 50]],
>                             Text[Style[b, 50]=
],
>                                  Text[S=
tyle["=", 50]],
>                                     =
Text[Style[Dynamic[f], 50]]}]]
>
>                   }]
>
>   ]
>                          ]
>
> In this program however, one needs to manually re-execute the program
> (e.g. via Shift+Enter) each time one wants to have a new execise. In
> order to avoid the manual restarting in addition to the above code I
> would like to include two buttons. First should restart the program, so
> that the task is renewed. The second button should close the panel when
> the game is finished. Thus, the program may be executed only once before
> the game starts. My idea to add a restart button the way shown below did
> not work:
>
> Label["aa"];
> Panel[DynamicModule[{a, b},
>               Clear[a, b, f];
>                   a = RandomInteger[{1, 4}];
>                                 b = Ran=
domInteger[{1, 4 - a}];
>                           f = "?";
>   g1 := (Clear[f]; f = a + b);
>   g2 := (Clear[f]; f = "?");
>
>   Column[{
>             Row[{
>
>               Dynamic@Button[Style[1, 30], If[a + b == =
1, g1, g2]],
>                   Dynamic@Button[Style[2, 30], If[a + b=
== 2, g1, g2]],
>                       Dynamic@Button[Style[3, 30], =
If[a + b == 3, g1, g2]],
>                           Dynamic@Button[Style[=
4, 30], If[a + b == 4,
> g1, g2]]
>
>                             }],
>
>                 Panel[Row[{Text[Style[a, 50]],
>                        Text[Style["+", 50]],
>                           Text[Style[b, 50]],
>                                Text[Style=
["=", 50]],
>                                     T=
ext[Style[Dynamic[f], 50]]}]],
>     Button["Restart", Goto["aa"]]
>
>                   }]
>
>   ]
>                          ]
>
> Could anybody give me an idea?
>
> Best, Alexei
>
> --
> Alexei Boulbitch, Dr., habil.
> Senior Scientist
>
> IEE S.A.
> ZAE Weiergewan
> 11, rue Edmond Reuter
> L-5326 Contern
> Luxembourg
>
> Phone: +352 2454 2566
> Fax:   +352 2454 3566
>
> Website:www.iee.lu
>
> This e-mail may contain trade secrets or privileged, undisclosed or other=
wise confidential information. If you are not the intended recipient and ha=
ve received this e-mail in error, you are hereby notified that any review, =
copying or distribution of it is strictly prohibited. Please inform us imme=
diately and destroy the original transmittal from your system. Thank you fo=