MathGroup Archive 2010

[Date Index] [Thread Index] [Author Index]

Search the Archive

Re: looping

  • To: mathgroup at smc.vnet.net
  • Subject: [mg106766] Re: [mg106704] looping
  • From: Leonid Shifrin <lshifr at gmail.com>
  • Date: Sat, 23 Jan 2010 07:30:56 -0500 (EST)
  • References: <201001210955.EAA16523@smc.vnet.net>

Hi Glenn,

You don't need a loop at all, if I understand your goal correctly. What you
need is a bit of Dynamic functionality. Hope this will get you started:

Clear[makeScorePanel, getScores, getCalled, getPrcntp, bpanel];
makeScorePanel[names : {__String}] :=
  Module[{text, n = RandomInteger[Length[names] - 1] + 1, studentInfo,
    classFlag = False, buttons, class, percent, next,
    score , called , prcnt},
   Clear[getScores, getCalled, getPrcntp, bpanel];
   score = called = prcnt = Table[0, {Length[names]}];
   getScores[] := score;
   getCalled[] := called;
   getPrcntp[] := prcnt;
   text := "Was the answer correct?";
   next := (called[[n]]++; n = RandomInteger[Length[names] - 1] + 1);
   class :=
    If[classFlag,
      Panel[Grid[
       Transpose[{names, score,
         IntegerPart[100*score/(called /. (0 -> 1))]/100.}
        ],
       Spacings -> 3, Dividers -> Center]],
     ""];
   buttons :=
    Row[{
      Button["    Yes    ", score[[n]]++; next],
      Button["Class", classFlag = ! classFlag],
      Button["    No    ", next]}];
   percent :=
    If[called[[n]] == 0, 0,
     IntegerPart[100*score[[n]]/called[[n]]]/100.];
   studentInfo :=
    Panel[Grid[
      {{"Name", names[[n]]},
       {"Times called", called[[n]]},
       {"# of correct answers", score[[n]]},
       {"Your %     ", percent}},
      Alignment -> Left]];
   bpanel[] :=
    Dynamic@Panel[Column[{
        studentInfo,
        text,
        buttons,
        class
        }]]];

The way to use: first call the makeScorePanel[] with your actual list of
names, then call bpanel[]:

makeScorePanel[{"apple", "bob", "cat", "dog", "ear", "frog", "greg",
  "hippo", "i9", "joe"}]

bpanel[]

You stop when you feel like it. You can also call   getScores[], getCalled[]
,  getPrcntp[] at any time to get the current state of these variables (if
you need that for further processing).

Regards,
Leonid



On Thu, Jan 21, 2010 at 12:55 PM, glenn kuhaneck <mcguyver128 at yahoo.com>wrote:

> this code is supposed to randomly select a student from a list, keep track
> of how many times he/she has been called, and how many answers they have
> gotten correct.
>
> I am having problems getting the following code to repeat on request: i
> have tried do loops, while loops, and labels none have worked.   please help
>
> These are the sample lists I am using for the code below
> name = ( {
>   {"apple"},
>   {"bob"},
>   {"cat"},
>   {"dog"},
>   {"ear"},
>   {"frog"},
>   {"greg"},
>   {"hippo"},
>   {"i9"},
>   {"joe"}
>  }); score = ({
>   {0},
>   {0},
>   {0},
>   {0},
>   {0},
>   {0},
>   {0},
>   {0},
>   {0},
>   {0}
>  }); called = ({
>   {0},
>   {0},
>   {0},
>   {0},
>   {0},
>   {0},
>   {0},
>   {0},
>   {0},
>   {0}
>  }); prcnt = ( {
>   {0},
>   {0},
>   {0},
>   {0},
>   {0},
>   {0},
>   {0},
>   {0},
>   {0},
>   {0}
>  } );
>
>
>
>
> this is the code to manipulate the lists created above
> repeat = "yes";
> c = "wrong";
> l = Length[name];
> n = RandomInteger[l - 1] + 1;
>
>
> Label[begin]
> "Student #"
> n
> name[[n]]
> called[[n]] += 1;
> "# times called"
> called
> "Old Score"
> score
> Input["Was the answer correct?", Button["Yes", c = "Right"]];
> If[c == "Right", score[[n]] += 1]; c
> "New Score"
> score
> "Your # of Correct Answers"
> score[[n]]
> "Your %"
> prcnt[[n]] = score[[n]]/called[[n]]
> "Class %"
> prcnt
> Input["Another ?", Button["No", repeat = "no"]];
> If[repeat == "yes", Goto[begin], Goto[end]];
> Label[end];
> prcnt[[n]]
>
>
>
> thank you for your assistance,
>  Mr. Glenn J. Kuhaneck
>
>



  • References:
    • looping
      • From: glenn kuhaneck <mcguyver128@yahoo.com>
  • Prev by Date: Re: Re: Re: Replace list element
  • Next by Date: Re: Re: NotebookGet/Read/EvaluateSelection Issues
  • Previous by thread: Re: looping
  • Next by thread: Re: Re: looping