If statement. Variable between 2 values (SOLVED)

Discuss how to use the Ren'Py engine to create visual novels and story-based games. New releases are announced in this section.
Forum rules
This is the right place for Ren'Py help. Please ask one question per thread, use a descriptive subject like 'NotFound error in option.rpy' , and include all the relevant information - especially any relevant code and traceback messages. Use the code tag to format scripts.
Post Reply
Message
Author
Exiscoming
Regular
Posts: 132
Joined: Tue Apr 29, 2014 5:37 pm
Contact:

If statement. Variable between 2 values (SOLVED)

#1 Post by Exiscoming »

Hey, quick (hopefully easy) question. How do I make an if statement activate if a variable is between 2 numbers? Example:

Code: Select all

if variable == 1-5:
"Hi, good to see you again!"
elif:
"Hello, I don't think we've met."
I know it's a dumb question, but I've been searching for over an hour, and simply don't know exactly what to look for.
Last edited by Exiscoming on Mon Mar 23, 2015 3:13 pm, edited 1 time in total.

philat
Eileen-Class Veteran
Posts: 1900
Joined: Wed Dec 04, 2013 12:33 pm
Contact:

Re: If statement. Variable between 2 values (Quick question)

#2 Post by philat »

Code: Select all

if 1 <= variable <=5:
    "something"
else:
    "something else"
You can also take out the = to have the comparison exclude the boundary numbers.

Exiscoming
Regular
Posts: 132
Joined: Tue Apr 29, 2014 5:37 pm
Contact:

Re: If statement. Variable between 2 values (Quick question)

#3 Post by Exiscoming »

Thank you! Is this the only way to do it though? I seem to have this idea that you could do something like:

Code: Select all

if variable == (1, 6):
   "Something happeneds."
Not sure if I just misremember or maybe it's from another code language. It's been a long time since I've tried Ren'py. :)

User avatar
Zetsubou
Miko-Class Veteran
Posts: 522
Joined: Wed Mar 05, 2014 1:00 am
Completed: See my signature
Github: koroshiya
itch: zetsuboushita
Contact:

Re: If statement. Variable between 2 values (Quick question)

#4 Post by Zetsubou »

Exiscoming wrote:Thank you! Is this the only way to do it though? I seem to have this idea that you could do something like:

Code: Select all

if variable == (1, 6):
   "Something happeneds."
Not sure if I just misremember or maybe it's from another code language. It's been a long time since I've tried Ren'py. :)
Looks like it's from another language. In python, that statement says "if variable is equal to the tuple (1,6)", so it certainly isn't what you're looking for.
Maybe you're thinking of:

Code: Select all

if variable in range(1, 6):
   "Something happeneds."
Finished games
-My games: Sickness, Wander No More, Max Massacre, Humanity Must Perish, Tomboys Need Love Too, Sable's Grimoire, My Heart Grows Fonder, Man And Elf, A Dragon's Treasure, An Adventurer's Gallantry
-Commissions: No One But You, Written In The Sky, Diamond Rose, To Libertad, Catch Canvas, Love Ribbon, Happy Campers, Wolf Tails

Working on:
Sable's Grimoire 2

https://zetsubou.games

Exiscoming
Regular
Posts: 132
Joined: Tue Apr 29, 2014 5:37 pm
Contact:

Re: If statement. Variable between 2 values (Quick question)

#5 Post by Exiscoming »

Maybe... either way it worked! Much obliged!

Exiscoming
Regular
Posts: 132
Joined: Tue Apr 29, 2014 5:37 pm
Contact:

Re: If statement. Variable between 2 values (Quick question)

#6 Post by Exiscoming »

Thanks again for the help, but I've stumbled upon another problem. Can I add an if statement inside of an if?

Example:

Code: Select all

    if 0 >= morale <= 5:
        "What?! No way!"
    elif 6 >= morale <= 10:
         $morale -= 1
         $bobHomework += 1
         "Fine I'll do it."
         if 1 >= bobHomework <= 10:
              "Bob looks dumb."
         elif 11 >= bobHomework <= 20:
              "Bob looks smart."
Right now it sometimes goes: "Bob looks smart." eventhough his bobHomework veriable is 1.

philat
Eileen-Class Veteran
Posts: 1900
Joined: Wed Dec 04, 2013 12:33 pm
Contact:

Re: If statement. Variable between 2 values (Quick question)

#7 Post by philat »

Because all your comparisons are the wrong way. 1 <= variable <= 10, not 1 >= variable <= 10.

User avatar
Alex
Lemma-Class Veteran
Posts: 3090
Joined: Fri Dec 11, 2009 5:25 pm
Contact:

Re: If statement. Variable between 2 values (Quick question)

#8 Post by Alex »

Try

Code: Select all

        if 1 <= bobHomework <= 10:
              "Bob looks dumb."
        elif 11 <= bobHomework <= 20:
              "Bob looks smart."
edit: I'm soooo sloooo...

Exiscoming
Regular
Posts: 132
Joined: Tue Apr 29, 2014 5:37 pm
Contact:

#9 Post by Exiscoming »

Oh that's it! But wait, doesn't <= mean less or equal to?

Example:
if <= 10 Variable <= 20
"Activate!"

Doesn't this read as anything lower or equal to 10 and everything lower or equal to 20 will activate it?
Say that the variable is 9. That's lower than 10 and will therefor activate, right?

User avatar
Zetsubou
Miko-Class Veteran
Posts: 522
Joined: Wed Mar 05, 2014 1:00 am
Completed: See my signature
Github: koroshiya
itch: zetsuboushita
Contact:

Re:

#10 Post by Zetsubou »

Exiscoming wrote:Oh that's it! But wait, doesn't <= mean less or equal to?

Example:
if <= 10 Variable <= 20
"Activate!"

Doesn't this read as anything lower or equal to 10 and everything lower or equal to 20 will activate it?
Say that the variable is 9. That's lower than 10 and will therefor activate, right?
You can't do that. What you're likely after is

Code: Select all

if 10 <= Variable <= 20:
    "Activate!"
So if Variable is greater than or equal to 10, and less than or equal to 20, it's true.
Finished games
-My games: Sickness, Wander No More, Max Massacre, Humanity Must Perish, Tomboys Need Love Too, Sable's Grimoire, My Heart Grows Fonder, Man And Elf, A Dragon's Treasure, An Adventurer's Gallantry
-Commissions: No One But You, Written In The Sky, Diamond Rose, To Libertad, Catch Canvas, Love Ribbon, Happy Campers, Wolf Tails

Working on:
Sable's Grimoire 2

https://zetsubou.games

Exiscoming
Regular
Posts: 132
Joined: Tue Apr 29, 2014 5:37 pm
Contact:

#11 Post by Exiscoming »

haha, thank you all for the help. I 'think' I get it now. And it seems to be working. :) I'll probably come up with more questions in the future, but for now thanks!

Post Reply

Who is online

Users browsing this forum: Ahrefs [Bot], Majestic-12 [Bot]