Problem with if statement and variables

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
qcastro
Newbie
Posts: 7
Joined: Sun Apr 03, 2016 5:06 pm
Contact:

Problem with if statement and variables

#1 Post by qcastro »

What goes wrong with this code? Please, I'm trying to figure out, but it seems impossible:

Code: Select all

label start:
    $ point = 0
    $ cor = none
    scene bg sala de prova
    if point > 0 and point <=10:
        $ cor = "white"
    elif point > 10 and point <=20:
        $ cor = "green"
    elif point > 20 and point <=30:
        $ cor = "blue"

    $ point += 15
    "The color is [cor]"
[/color]

But it always shows only the "cor is none"

What am I doing wrong?
Last edited by qcastro on Sun Apr 03, 2016 5:43 pm, edited 2 times in total.

User avatar
Donmai
Eileen-Class Veteran
Posts: 1960
Joined: Sun Jun 10, 2012 1:45 am
Completed: Toire No Hanako, Li'l Red [NaNoRenO 2013], The One in LOVE [NaNoRenO 2014], Running Blade [NaNoRenO 2016], The Other Question, To The Girl With Sunflowers
Projects: Slumberland
Location: Brazil
Contact:

Re: Problem with if statement and variables

#2 Post by Donmai »

qcastro wrote:What goes wrong with this code?
If you don't show it using CODE tags, to preserve your indentation, it's very difficult to tell.
Image
No, sorry! You must be mistaking me for someone else.
TOIRE NO HANAKO (A Story About Fear)

qcastro
Newbie
Posts: 7
Joined: Sun Apr 03, 2016 5:06 pm
Contact:

Re: Problem with if statement and variables

#3 Post by qcastro »

Donmai wrote:
qcastro wrote:What goes wrong with this code?
If you don't show it using CODE tags, to preserve your indentation, it's very difficult to tell.
Sorry, I corrected the indentation. But what is wrong?

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: Problem with if statement and variables

#4 Post by Zetsubou »

You're setting point to 0, and the lowest check you do is to make sure it's above 0.
So either raise point to 1, or change the logic.
eg.

Code: Select all

if point >= 0 and point <=10:
Also, you should change

Code: Select all

$ cor = none
to either

Code: Select all

$ cor = 'none'
or

Code: Select all

$ cor = None
Looking at your code, I'm guessing you want the first one.
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

User avatar
trooper6
Lemma-Class Veteran
Posts: 3712
Joined: Sat Jul 09, 2011 10:33 pm
Projects: A Close Shave
Location: Medford, MA
Contact:

Re: Problem with if statement and variables

#5 Post by trooper6 »

If you follow the code through logically step by step, you will see what the problem is.

Let's look at your code line by line (I'm adding numbers so that we can talk line by line...but these numbers shouldn't be in the actual code).

Code: Select all

1) label start:
2)     $ point = 0
3)     $ cor = none
4)     scene bg sala de prova
5)     if point > 0 and point <=10:
6)         $ cor = "white"
7)     elif point > 10 and point <=20:
8)         $ cor = "green"
9)     elif point > 20 and point <=30:
10)         $ cor = "blue"

11)     $ point += 15
12)     "The color is [cor]"
Line 1 the game starts.
Line 2 you assign point to 0.
Line 3 you assign cor to none.

As Zestsubou notes that should be "None" or None.
But I would also recommend using current best practices and assigning those values using default. So like so:

Code: Select all

1) default point = 0
2) default cor = "None"

3) label start:
4)     scene bg sala de prova
5)     if point > 0 and point <=10:
6)         $ cor = "white"
7)     elif point > 10 and point <=20:
8)         $ cor = "green"
9)     elif point > 20 and point <=30:
10)         $ cor = "blue"

11)     $ point += 15
12)     "The color is [cor]"
Okay moving on.
Line 4 you set the scene.
Line 5-10 you check to see what value point is. Let's do the check.
Line 5, is point greater than 0 and less than or equal to 10? No, because it is 0. Line 6 doesn't happen.
Line 7, is point greater than 10 and less than or equal to 20? No, because it is 0. Line 8 doesn't happen.
Line 9, is point greater than 20 and less than or equal to 30? No, because it is 0. Line 10 doesn't happen.
So after this section, cor is still "None" and point is still 0.

Line 11, you add 15 to point, so point is now 15 and cor is still "None"--because you haven't done anything to cor.
Line 12, since cor is still "None", this line will print: "The color is None."

Do you see the error now? You assign a color to cor before you add points to point. After you add points to point, you don't do your if/else check again.

Depending on exactly what you are doing (screens are a bit different), you need to check the points and assign color each time before you need to print the color. This can be made easier by making a method...which is a block of code you plan on repeating quite often.

Observe this version of the code:

Code: Select all

default point = 0
default cor = "None"

init python:
    def colorCheck():
        global point
        global cor
        if point > 0 and point <=10:
            cor = "white"
        elif point > 10 and point <=20:
            cor = "green"
        elif point > 20 and point <=30:
            cor = "blue"
        

label start:
    scene bg sala de prova
    $ colorCheck()
    "The color is [cor], it should be None."
    
    $ point += 5
    $ colorCheck()
    "The color is [cor], it should be white."
    
    $ point += 10
    $ colorCheck()
    "The color is [cor], it should be green."
    
    $ point += 10
    $ colorCheck()
    "The color is [cor], it should be blue."
Now, if you are doing screens it is different. But I hope you can see the what was wrong with your original code.
A Close Shave:
*Last Thing Done (Aug 17): Finished coding emotions and camera for 4/10 main labels.
*Currently Doing: Coding of emotions and camera for the labels--On 5/10
*First Next thing to do: Code in all CG and special animation stuff
*Next Next thing to do: Set up film animation
*Other Thing to Do: Do SFX and Score (maybe think about eye blinks?)
Check out My Clock Cookbook Recipe: http://lemmasoft.renai.us/forums/viewto ... 51&t=21978

qcastro
Newbie
Posts: 7
Joined: Sun Apr 03, 2016 5:06 pm
Contact:

Re: Problem with if statement and variables

#6 Post by qcastro »

trooper6 wrote:If you follow the code through logically step by step, you will see what the problem is.

Let's look at your code line by line (I'm adding numbers so that we can talk line by line...but these numbers shouldn't be in the actual code).
...
Thanks!
This is what I intend to do, but I didn't remember the python method (and how to implement a global variable). I read some python tutorial, but it was not much, to solve alone all the problems. You really caught what I need, a method to call and change de color name (something like "black belt").

Thank you very much!!

qcastro
Newbie
Posts: 7
Joined: Sun Apr 03, 2016 5:06 pm
Contact:

Re: Problem with if statement and variables

#7 Post by qcastro »

Zetsubou wrote:You're setting point to 0, and the lowest check you do is to make sure it's above 0.
So either raise point to 1, or change the logic.
eg.

Code: Select all

if point >= 0 and point <=10:
Also, you should change

Code: Select all

$ cor = none
to either

Code: Select all

$ cor = 'none'
or

Code: Select all

$ cor = None
Looking at your code, I'm guessing you want the first one.
Thanks to your attention and answer.

Post Reply

Who is online

Users browsing this forum: No registered users