Services & Resources / Wolfram Forums / MathGroup Archive
-----

MathGroup Archive 2010

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

Search the Archive

Re: Re: looping

  • To: mathgroup at smc.vnet.net
  • Subject: [mg106841] Re: [mg106766] Re: [mg106704] looping
  • From: DrMajorBob <btreat1 at austin.rr.com>
  • Date: Sun, 24 Jan 2010 05:45:32 -0500 (EST)
  • References: <201001210955.EAA16523@smc.vnet.net>
  • Reply-to: drmajorbob at yahoo.com

Thanks! I've just noticed something I don't understand, however.

How can displayPanel[] be used outside the makeScorePanel function, when  
it uses studentInfo, text, buttons, and class -- all of which are local to  
the Module??

Bobby

On Sat, 23 Jan 2010 14:57:25 -0600, Leonid Shifrin <lshifr at gmail.com>  
wrote:

> Hi Bobby,
>
> I agree - your modification makes it more elegant. I  particularly liked
> your  use of indexed variables and RandomChoice.
>
> Cheers,
> Leonid
>
>
> On Sat, Jan 23, 2010 at 12:48 PM, DrMajorBob <btreat1 at austin.rr.com>  
> wrote:
>
>> I'd style this a bit differently:
>>
>> Clear[makeScorePanel];
>> makeScorePanel[names : {__String}] :=
>>  Module[{text, k = Length@names, name = RandomChoice@names,
>>    studentInfo, classFlag = True, buttons, class, percent, next,
>>    correct, questions},
>>
>>   "internal functions";
>>   correct[_] = 0; questions[_] = 0;
>>   percent[name_] /; questions[name] == 0 = 0;
>>   percent[name_] := Round[100*correct[name]/questions[name]];
>>
>>   "display elements";
>>
>>   text = "Was the answer correct?";
>>   next := (questions[name]++; name = RandomChoice@names);
>>
>>   class :=
>>    If[classFlag,
>>     Panel[Grid[
>>       Prepend[Transpose[{names, getQuestionCount[],
>>          getCorrectCount[], getPercent[]}], {"Name", "Questions",
>>         "# correct", "% correct"}], Spacings -> 3,
>>       Dividers -> Center]], ""];
>>   buttons :=
>>    Row[{Button["    Yes    ", correct[name]++; next],
>>      Button["    No    ", next],
>>      Button[If[classFlag, "Hide Roster", "Show Roster"],
>>       classFlag = ! classFlag]}];
>>   studentInfo :=
>>    Panel[
>>     Grid[{{"Name", name}, {"Questions",
>>        questions@name}, {"Correct answers",
>>        correct@name}, {"% correct", percent@name}},
>>      Alignment -> Left]];
>>
>>   "exported functions";
>>   Clear[getCorrectCount, getQuestionCount, getPercent,
>>    displayPanel];
>>   getCorrectCount[] := correct /@ names;
>>   getQuestionCount[] := questions /@ names;
>>   getPercent[] := percent /@ names;
>>   displayPanel[] :=
>>
>>    Dynamic@Panel[Column[{studentInfo, text, buttons, class}]]
>>   ];
>> makeScorePanel[{"apple", "bob", "cat", "dog", "ear", "frog", "greg",
>>  "hippo", "i9", "joe"}]
>> displayPanel[]
>>
>> Bobby
>>
>>
>> On Sat, 23 Jan 2010 06:30:56 -0600, Leonid Shifrin <lshifr at gmail.com>
>> wrote:
>>
>>  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
>>>>
>>>>
>>>>
>>>
>>>
>>
>> --
>> DrMajorBob at yahoo.com
>>


-- 
DrMajorBob at yahoo.com


  • References:
    • looping
      • From: glenn kuhaneck <mcguyver128@yahoo.com>
  • Prev by Date: Re: Journals dying?, apparently rather slowly (was ,
  • Next by Date: Re: Journals dying?, apparently rather slowly (was , I->-I)
  • Previous by thread: Re: Re: looping
  • Next by thread: Re: Re: looping