Help! Expected no more tokens, found: NAMED_OPERATOR [and]

I’m having trouble with: Error: ch1 line 1304: Invalid expression at char 41, expected no more tokens, found: NAMED_OPERATOR [and]

I don’t understand the meaning of it.

Here is the code.

Nrein's eyes gaze at you with a small smile on her lips.
*if (eyes = "blue") and (hair = "blonde") and (gender = "female")
    "If Alara weren't with me right now, I would have thought that you were her." Your mother's face hardens at her words. She really hates when someone says that you resemble Nrein or her daughter.    
*if (eyes = "blue") and (hair = "blonde") and (gender = "male")
    "For a second, I thought you were my son, but he's a lot taller than you." Your mother's face hardens at her words. She really hates when someone says that you resemble Nrein's looks or her son's.    
*if (eyes = "blue") and (hair != "blonde")
    "Beautiful blue eyes you have, ${girl}. If only you had my hair, then you would have looked identical to my children. Your mother's face hardens at her words. She really hates when someone brings up your blue eyes.
*if (hair = "blonde") and (eyes != "blue")
    "Beautiful golden hair you have, ${girl}. If only you had my eyes, then you would have looked identical to my children. Your mother's face hardens at her words. She really hates when someone brings up your blonde hair.
2 Likes

*if in choice script can only compare 2 parameters at a time. That means that you have to use parantheses to explicitly mark which two pairs should be compared. when you have 3 parameters, it should look something like:

*if (((a = “value”) and (b=“something”)) and (c = false))

Thia way, the first two statements are compared, then the result of the comparison is compared with the third parameter.

5 Likes

This is really confusing. (Sorry, I’m dumb with coding)

Should it be like this?

*if (((eyes = “blue”) and (hair = “blonde”)) and (gender = “female”))

And what should I do if I need to use more than three?

Thank you so much! :smiling_face_with_three_hearts:

4 Likes

Yes your code is correct. Let me break down the process so it’s easier to follow.

Imagine that when you have an if with 3 parameters, there are two consecutive ifs happening. Imagine that we could store the result of evaluating an expression in a variable. The following examples are pseudocode:

val result = if (a) and (b) and (c)

can be writen as

var eval = if(a) and (b)
val result = if (eval) and (c)

this way, the if always has 2 parameters.

Lets extrapolate this for CS.

If you want to compare n conditions, you will need n-1 pairs of comparisons marked by (). So for your question, lets do an example for 4.

4-1 = 3 pairs of comparisons.

These are the 4 conditions that have to be satisfied
a= “3”
b= “test”
c = false
d = “31”

  1. Lets pair the first params

(a = “3”) and (b=“test”)

  1. wrap it all in () so we can compare it with the next param

((a = “3”) and (b = “3”))

  1. take step 2 and compare with the 3rd condition

((a = “3”) and (b = “3”)) and (c = false)

  1. wrap it all in ()

(((a = “3”) and (b = “3”)) and (c = false))

  1. take step 4 and compare it with the last param

(((a = “3”) and (b = “3”)) and (c = false)) and d = (“31”)

  1. wrap it in () and you have your if statement

*if ((((a = “3”) and (b = “3”)) and (c = false)) and d = (“31”))

edit: think about how you do an addition in your head

1 + 2 + 5 + 8.

first you do

1+ 2 = 3

then

3 + 5 + 8

3 + 5 = 8

8 + 8 = 16

5 Likes

Sorry for annoying you again, but when I tried to use your code, I got a different error at the same line.

RANDOMTEST FAILED: Error: ch1 line 1304: Invalid use of curly smart quote: “ Use straight quotes " instead

1 Like

im writing on mobile and the quotes are curly instead of straight. just replace “ with " as the error message says

2 Likes

Another way to see it is that you’re trying to do this:

*if a = b
  *if c = d
    *if e = f
      *if g = h

But into a single line. So you’re only comparing 2 values at a time, you can’t add all the comparisons together like a=b and c=d and e=f and g=h so you have to divide them two by two.

first ( (a=b), the result of that goes with the result of (c=d), ) the result of both goes with the result of (e=f) and so on.

2 Likes

Just to say, while this code works fine as you shared it:

*if (((eyes = "blue") and (hair = "blonde")) and (gender = "female"))

It’s actually condensed one step beyond what you need. CS can handle two things at a time, with anything in parentheses counting as one thing. By slapping that last set of parentheses to put eyes, hair, and gender in a single package, you’ve ultimately given CS just “one thing” to chew on. That works fine, but it does mean you’ve got one more set of parentheses in there than you strictly need.

So this should work just as well:

*if ((eyes = "blue") and (hair = "blonde")) and (gender = "female")

As should this:

*if (eyes = "blue") and ((hair = "blonde") and (gender = "female"))

In the first one, you’re using parentheses to turn “eyes + hair” into “one thing” for CS to parse; in the second, “hair + gender” have become “one thing.” Ultimately either way you’re sticking to the rule of giving CS no more than two things total to chew on at a time inside any parentheses or in any *command.

If you want to add a fourth condition, all of equal importance, you could either give CS two groups of two to chew on:

*if ((eyes = "blue") and (hair = "blonde")) and ((tentacles = "squamous") and (gender = "female"))

or use nested parentheses like in your original example:

*if (((eyes = "blue") and (hair = "blonde")) and (tentacles = "squamous")) and (gender = "female")

This all assumes everything is equally relevant in the outcome, so it’s all "and"s in the code. If you want “or,” you have to be a little more careful to make sure that your groupings-into-two make sense.

Say that we’re in a world where either blue eyes/blond hair or scaly tentacles are shockingly rare for women, and would get a surprised reaction from the local villagers. You need to make sure your “or” is in the right place relative to your parentheses. All the code below works, but works quite differently:

*if (((eyes = "blue") and (hair = "blonde")) or (tentacles = "squamous")) and (gender = "female")

Villagers have a shocked reaction to either Nordic or tentacled women.

*if ((eyes = "blue") and (hair = "blonde")) or ((tentacles = "squamous") and (gender = "female"))

Villagers are shocked by tentacled women or by blue eyed-blond haired people of any sex.

*if (eyes = "blue") and ((hair = "blonde") or ((tentacles = "squamous") and (gender = "female")))

Villagers are shocked either by blue-eyed blonde people or by blue-eyed tentacle women.

Have a look through those examples and when you get them, you’ll be well on your way. :slight_smile:

3 Likes

This topic was automatically closed 24 hours after the last reply. If you want to reopen your WiP, contact the moderators.