Howdy, Stranger!

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

Code to print something if a variable is comprised between two numbers

Hi Twine community!


I started using Twine to prototype the investigation game I am developing.

I am pretty much a newbie when it comes to coding and I need your help as I want to have certain messages printed depending on the value of my variable $trust.

My variable works. I tested it with a counter displaying how it evolves, and I can print messages when it is above or below a certain value.
I use this (if: $trust < X)[Bla bla bla...]

However, I cannot find the way to display a message when it is comprised between two values.

What would be the code to display something like "Your trust level is between 0 and 10" when my $trust is comprised 0 and 10?


Thank you!  :)

Comments

  • (You should always specify which story format you are using, as they differ in syntax, features, and behavior.)

    Anyway, it's fairly obvious that you're using Harlowe.  The following should do what you want when $trust is in the range 010 (inclusive):

    (if: $trust >= 0 and $trust <= 10)[Your trust level is between 0 and 10]
    By using the it keyword, which is shorthand for the leftmost operand in an expression, you could write that more concisely as:

    (if: $trust >= 0 and it <= 10)[Your trust level is between 0 and 10]
  • Hi and thanks a lot for helping out.

    Here is my code:

    (if: $trust > 29)[Trust level above 29]
    (if: $trust => 20 and $trust <= 29)[Trust level between 20 and 29]
    (if: $trust => 10 and $trust <= 19)[Trust level between 10 and 19]
    (if: $trust => 0 and $trust <= 9)[Trust level between 0 and 9]
    (if: $trust < 0)[Trust level below 0]

    I ran a test in which my $trust was worth 10, and I got this:

    Trust level between 20 and 29
    Trust level between 10 and 19
    Trust level between 0 and 9

    While I should only have "Trust level between 10 and 19"...

    ???
  • The greater than or equals to symbol >= in your example is around the wrong way, the > goes before the =

    Try this:

    (set: $trust to 10)

    (if: $trust > 29)[Trust level above 29]
    (elseif: $trust >= 20 and $trust <= 29)[Trust level between 20 and 29]
    (elseif: $trust >= 10 and $trust <= 19)[Trust level between 10 and 19]
    (elseif: $trust >= 0 and $trust <= 9)[Trust level between 0 and 9]
    (elseif: $trust < 0)[Trust level below 0]
    Because your conditional tests are mutually exculsive (eg. they don't over lap) you can use the (elseif:) macro on the second through fifth test.
  • Great, thanks a lot for your help!  :)
Sign In or Register to comment.