Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Sugarcube 2.7.0 If conditionals with two textboxes and multiple variables

Hello all,

I've got a couple of textboxes where users enter answers to questions.
31. Write the <b>first name</b> of one student who //passed// the session: 
<<textbox "$q41" "">>

32. Write the <b>first name</b> of one student who //failed// the session. 
<<textbox "$q42" "">><<button [[Submit|Partner Finalize FGR 3]]>><</button>>

The user has the answers, and can enter them in. There are several possible answers to each question, so I'm checking like this:
<<if $q41 eq "Shannon" or $q41 eq "Muriel" or $q41 eq "Marion" or "Debra" or $q41 eq "Ira" or $q41 eq "Wendell" or $q41 eq "Kelly" or $q41 eq "Peter" or $q41 eq "Aubrey" or $q41 eq "Perry" or $q41 eq "Troy">>
	<<if $q42 eq "Rosie" or $q42 eq "Allen" or $q42 eq "Marguerite">>
	<<set $player.experience to $player.experience + 5>>
<else>>Nope! <<back>><<audio "error" play>>        
<</if>>
<</if>>

I originally had it setup as one <<if>> conditional with "and" between $q41 and $q42, but that didn't work either. The problem is that if I enter any two names listed in either textbox, the result is accepted as correct and experience is gained by the player.

For example, if I enter Rosie in $q41 textbox and Allen in the $q42 textbox, (One value incorrect, one value correct) the answers are counted as correct and experience is awarded.

On the other hand, if I enter Rosie in $q41 and Shannon in $q42, (meaning both values are incorrect for the accepted answers), it returns a false value. Is there a way that I can more accurately assess the validity of both variables, instead of just one?

Thank you!

Comments

  • edited August 2016
    The logical-AND operator has higher precedence than the logical-OR operator, so you should use parenthesis to group the two logical-OR subexpressions. For example:
    <<if
        ($q41 eq "Shannon" or $q41 eq "Muriel" or $q41 eq "Marion" or "Debra" or $q41 eq "Ira" or $q41 eq "Wendell" or $q41 eq "Kelly" or $q41 eq "Peter" or $q41 eq "Aubrey" or $q41 eq "Perry" or $q41 eq "Troy")
    and ($q42 eq "Rosie" or $q42 eq "Allen" or $q42 eq "Marguerite")
    >>\
    <<set $player.experience to $player.experience + 5>>
    <else>>\
    Nope! <<back>><<audio "error" play>>        
    <</if>>
    

    A somewhat easier to read alternative would be to use two temporary variables to hold arrays of the valid answers and the <Array>.contains() method to search them for the given answers. For example:
    <<set
    	_a41 to ["Shannon", "Muriel", "Marion", "Debra", "Ira", "Wendell", "Kelly", "Peter", "Aubrey", "Perry", "Troy"],
    	_a42 to ["Rosie", "Allen", "Marguerite"]
    >>\
    <<if _a41.contains($q41) and _a42.contains($q42)>>\
    <<set $player.experience to $player.experience + 5>>
    <else>>\
    Nope! <<back>><<audio "error" play>>        
    <</if>>
    
  • Thanks very much. I tried the first method first, intending to use arrays after I could get the regular variable and if statements to work.

    I did it the way you first suggested and got mixed results. Here's what I entered in each textbox and the result:
    - $q41 true, $q42 true, returns true
    - $q41 blank, $q42 true, returns true
    - $q41 false (random text input), $q42 true, returns true
    - $q41 true, $q42 blank, returns false
    - $q41 true, $q42 false (random text input), returns false
    - $q41 false, $q42 false, returns false

    It seems like the logic is looking only at whether or $q42 is true, and is almost ignoring $q41. Any ideas why this may be? Thanks again for all your support.
  • The condition for "Debra" is broken. Instead of the correct:
    … or $q41 eq "Debra" or …
    
    It is just:
    … or "Debra" or …
    
    Which will cause the $q41 subexpression to always yield true.

    Errors like that are one of the reasons I suggested using arrays.
  • Good eye. Many thanks!
Sign In or Register to comment.