WeBWorK Problems

Declaring PopUp variables within if-else conditionals

Declaring PopUp variables within if-else conditionals

by tim Payer -
Number of replies: 2

Greetings Webwork Forum!

Am I wrong to include possible Popup answers in an if-else condition?

I am not clear on the meaning of the error message response of:

The correct choice must be one of the PopUp menu items at line 2 of (eval 6952)

   Died within Value::Error called at line 100 of [PG]/macros/parserPopUp.pl

   from within parser::PopUp::new called at line 2 of (eval 6952)

   from within main::PopUp called at line 67 of (eval 6714)

 

Does this mean that the if-else conditions were not met that there would be no assignment of an answer for PopUp3 ?  Or is there an advisement against using PopUp answers within if-else conditions?

Thanks for any guidance you can share with me,

Tim

 Here is the code block:

# DESCRIPTION   Probability calculation.

# Use correct notation and sample space

# to find the answer from two, and then three random draws.

# WeBWorK problem written by TimPayer <tsp1@humboldt.edu>

# ENDDESCRIPTION

 

## DBsubject(Probability)

## DBchapter(Random variables)

## DBsection(Expectation)

## Institution(Humboldt State University)

## Author(Tim Payer)

## KEYWORDS(probability, translate, notation)

DOCUMENT();

 

loadMacros(

"PGstandard.pl",

"PGunion.pl",

"MathObjects.pl",

"parserPopUp.pl",

"PGML.pl",

"PGcourse.pl",

);

 

#Text(beginproblem());   #uncomment

 

install_problem_grader(~~&std_problem_grader);

$showPartialCorrectAnswers = 1;

 

Context("Numeric");

 

# create three arrays of the same length "in parallel"

# Using "hashes" we have 30 word triplets coordinating the names of

# singular, plural forms for a given bird with its flock name.

 

$bird = list_random('goose', 'crow', 'starling', 'penguin','quail','hawk','finch','parrot', 'woodpecker', 'dove','lark', 'swallow', 'sparrow', 'stork','peacock', 'owl', 'jay', 'chicken', 'turtledove', 'turkey', 'magpie', 'raven', 'nightingale', 'snipe', 'heron', 'mallard', 'duck', 'pheasant', 'coot', 'plover');

%birds = (goose=>'geese', crow=>'crows', starling=>'starlings', penguin=>'penguins', quail=>'quails',hawk=>'hawks',finch=>'finches',parrot=>'parrots', woodpecker=>'woodpeckers', dove=>'doves', lark=>'larks',swallow=>'swallows', sparrow=>'sparrows', stork=>'storks', peacock=>'peacocks', owl=>'owls', jay=>'jays', chicken=>'chickens', turtledove=>'turtledoves', turkey=>'turkeys', magpie=>'magpies', raven=>'ravens', nightingale=>'nightingales', snipe=>'snipes', heron=>'herons' , mallard=>'mallards', duck=>'ducks', pheasant=>'pheasants', coot=>'coots', plover=>'plovers' );

%flock= (goose=>'gaggle', crow=>'murder', starling=>'murmuration', penguin=>'colony', quail=>'bevy',hawk=>'cast',finch=>'charm', parrot=>'company',woodpecker=>'descent', dove=>'dole', lark=>'exaltation', swallow=>'flight', sparrow=>'host', stork=>'muster', peacock=>'ostentation', owl=>'parliament', jay=>'party', chicken=>'peep', turtledove=>'pitying', turkey=>'rafter', magpie=>'tidings', raven=>'unkindness', nightingale=>'watch',  snipe=>'wisp', heron=>'siege', mallard=>'sord', duck=>'raft', pheasant=>'nye', coot=>'cover', plover=>'congregation');

 

$event = list_random('more than one', 'at least one', 'at most one', 'not every');

 

#@bird = ('goose','crow','starling');  ##This format needs Version 11 or better.

#@birds = ('geese','crows','starlings');

#@flock = ('gaggle','murder','murmuring');

#$i = random(0, 2, 1); # choose an array index at random

 

$popup1 = PopUp(

["probability notation", "P(V = 0)", "P(V = 1)", "P(V = 2)", "P(V  > 0)", "P(V  > 1)","P(V < 2)",], "P(V = 2)");

 

$popup2 = PopUp(

["probability notation", "P(V = 0)", "P(V = 1)", "P(V = 2)", "P(V  > 0)", "P(V  > 1)","P(V < 2)",], "P(V  > 0)");

 

$popup4 = PopUp(

["draws?", "one", "two", "three",], "two");

$popup5 = PopUp(

["draws?", "one", "two", "three",], "three");

$v = random(9,49,1);

$ans1 =Compute("$v*$v/100");

$ans2 = Compute("(2*$v-$v*$v)/100");

if($event == "more than one"){

$ans3 =$v**2*(3-2*$v);

$popup3 = PopUp( ["probability notation", "P(V = 0)", "P(V = 1)", "P(V = 2)", "P(V = 3)","P(V  > 0)", "P(V > 1)","P(V < 2)","P(V < 3)"], "P(V  > 1)");

} elsif ($event == "at least one") {

$ans3=1-(1-$v)**3;

$popup3 = PopUp(["probability notation", "P(V = 0)", "P(V = 1)", "P(V = 2)", "P(V = 3)","P(V  > 0)", "P(V > 1)","P(V < 2)","P(V < 3)"], "P(V  > 0)");          

} elsif ($event == "at most one") {

$ans3=(1+2*$v)*(1-$v)**2;

$popup3 = PopUp(["probability notation", "P(V = 0)", "P(V = 1)", "P(V = 2)", "P(V = 3)","P(V  > 0)", "P(V > 1)","P(V < 2)","P(V < 3)"], "P(V  < 2)");          

} elsif ($event == "not every") {

$ans3=1-$v**3;

$popup3 = PopUp(["probability notation", "P(V = 0)", "P(V = 1)", "P(V = 2)", "P(V = 3)","P(V  > 0)", "P(V > 1)","P(V < 2)","P(V < 3)"], "P(V < 3)");          

}

 

# problems with if-then for 1c depending on Popups

BEGIN_PGML

1. The specific name for a flock of [@$birds{$bird}@]  is described as a "[@$flock{$bird}@]" of [@$birds{$bird}@].  

Consider the case where [$v]% of a [@$flock{$bird}@] of [@$birds{$bird}@] carry a bird flu virus. If two [@$birds{$bird}@] are drawn at random from the [@$flock{$bird}@], find the following probabilities but first complete the following event variable declaration: 

Let V = The number of [@$birds{$bird}@] with the bird flu virus taken from  [___]{$popup4} random draw(s) from the [@$flock{$bird}@].

1a.) What is the probability of randomly drawing two [@$birds{$bird}@] from the [@$flock{$bird}@] that both have the bird flu virus? Choose the correct probability notation below and then calculate its associated probability with fourth decimal accuracy. 

[___]{$popup1}  =  [______]{$ans1}

1b.) What is the probability of randomly drawing two [@$birds{$bird}@] such that at least one [$bird] has the bird flu virus? Choose the correct probability notation below and then calculate its associated probability with fourth decimal accuracy. 

Hint: Let N = the event of a [$bird] not having the bird flu virus. Then construct a sample space of all the possible combinations of V and N.

[___]{$popup2} = [______]{$ans2}

1c.) Consider the case where three [@$birds{$bird}@] are drawn at random from the [@$flock{$bird}@]. What is the probability that [$event] [$bird] has the bird flu virus? First complete the declaration of the event variable below:

Let V = The number of [@$birds{$bird}@] with the bird flu virus taken from  [___]{$popup5} random draw(s) from the [@$flock{$bird}@].

Choose the correct probability notation below and then calculate its associated probability with fourth decimal accuracy. Hint: Let N = the event of a [$bird] not having the bird flu virus. Then construct a sample space of all the possible combinations of V and N.

[___]{$popup3} = [______]{$ans3}

END_PGML

BEGIN_PGML_SOLUTION

The correct answers are coming....:

END_PGML_SOLUTION

ENDDOCUMENT();             

 

 

 

In reply to tim Payer

Re: Declaring PopUp variables within if-else conditionals

by Danny Glin -
The error message is telling you that in your PopUp assignment, the string you have listed as the correct answer is not one of the choices.

It looks like the problem is in the following line:
$popup3 = PopUp(["probability notation", "P(V = 0)", "P(V = 1)", "P(V = 2)", "P(V = 3)","P(V  > 0)", "P(V > 1)","P(V < 2)","P(V < 3)"], "P(V  < 2)");     

In the list of possible answers, you have "P(V < 2)" with one space between V and <, whereas in the correct answer there are two spaces.  Because WeBWorK can't find this string in the list of drop-down options, it spits the error.

When I set these up, I use the following construction, which is a little less typing (or copy and paste)

if($event == "more than one"){

$ans3 =$v**2*(3-2*$v);

$popup3ans = "P(V  > 1)";

} elsif ($event == "at least one") {

$ans3=1-(1-$v)**3;

$popup3ans = "P(V  > 0)";

}

$popup3 = PopUp( ["probability notation", "P(V = 0)", "P(V = 1)", "P(V = 2)", "P(V = 3)","P(V  > 0)", "P(V > 1)","P(V < 2)","P(V < 3)"], $popup3ans);

Danny

In reply to Danny Glin

Re: Declaring PopUp variables within if-else conditionals

by tim Payer -
Thanks so much Danny,

I neglected to watch for that extra space.

I like your more concise use of the popup variable, and will use it.

Tim