if statement always false

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
Fire247
Newbie
Posts: 4
Joined: Fri Jun 15, 2018 7:02 pm
Contact:

if statement always false

#1 Post by Fire247 »

Hello, this is my first post here, so I apologize beforehand for any mistakes.

I didn't find an answer to my question anywhere on the internet and I'm slowly starting to get frustrated with the development of my school project. My problem is that every if statement always comes out false when I add "==" to the condition. Sorry that some stuff is written in German but variable names shouldn't really matter. Also, I just began programming with Python/RenPy about two days ago, so I'm a total noob. Help would be greatly appreciated! :)

Code: Select all

label addieren:
        e "Okay! Schreibe jederzeit 'Beenden' um aufzuhören."
        label addierenskip:
        $ a = renpy.random.randint(0, 10)
        $ b = renpy.random.randint(0, 10)
        $ del a
        $ del b
        $ a = renpy.random.randint(0, 10)
        $ b = renpy.random.randint(0, 10)
        $ summe = (a+b)
        $ inputzahl = renpy.input("Was ist [a] + [b]?")
        if inputzahl == "Beenden":
            e "Verstanden"
            jump anfang
        if summe == inputzahl:
            e "Richtig! +1 Punkt!"
            $ score += 1
            jump addierenskip
        else:
            e "Leider falsch."
            jump addierenskip

User avatar
mitoky
Veteran
Posts: 316
Joined: Sat Feb 07, 2015 9:12 pm
Projects: The Purring Demon's Love, circus eterie
Contact:

Re: if statement always false

#2 Post by mitoky »

I could be wrong on why this is the case but i think its because the text input is a string while the number is an integer. (i hope someone knows how to explain this better as i am not good with this myself)

A solution which works is change this:

Code: Select all

$ inputzahl = renpy.input("Was ist [a] + [b]?")
into this:

Code: Select all

    python:
        inputzahl = int(renpy.input("Was ist [a] + [b]?"))
Again, if someone better with coding could take a look it would be appreticiated owo)b

Und viel Glück!

kivik
Miko-Class Veteran
Posts: 786
Joined: Fri Jun 24, 2016 5:58 pm
Contact:

Re: if statement always false

#3 Post by kivik »

So mitoky's nailed it I think that the issue is because renpy.input returns a string, so you'd be comparing "10" == 10 for example and they don't equal.

Casting the value into an int would work: int("10") == 10

However, if you cast the renpy.input directly as an int, then this statement won't ever work:

Code: Select all

        if inputzahl == "Beenden":
Since inputzahl won't ever be a string. The other problem is that the player can input other nonsensical string that cannot be converted into an int, e.g. "orange". int("orange") would throw an error and crash your game for example.

So I'd suggest the following:

Code: Select all

    python:
        a = renpy.random.randint(0, 10)
        b = renpy.random.randint(0, 10)
        del a
        del b
        a = renpy.random.randint(0, 10)
        b = renpy.random.randint(0, 10)
        summe = (a+b)
        inputzahl = renpy.input("Was ist [a] + [b]?").strip()
    if inputzahl.lower() == "beenden":
        e "Verstanden"
        jump anfang
    if str(summe) == inputzahl:
        e "Richtig! +1 Punkt!"
        $ score += 1
        jump addierenskip
    else:
        e "Leider falsch."
        jump addierenskip
First of all, instead of having so many python lines with $, just put them inside a python block :D

I've tweaked the input function to .strip() your input from any spaces before or after. This is generally good habit, because I don't know why - but people have a habit of putting space after everything! This will remove the space for you.

Next I'm testing the lower case version of inputzahl against the lowercase version of "beenden" - this means "Beenden", "BEENDEN" and any other variations of letter casing will match just fine.

Then, instead of converting your input into an int, why not convert the summe variable into a string? the str() function does that - and it should work.

I also noticed you've got some weird indentation with your labels at the start - if you mean to use local labels, please check the correct usage here: https://www.renpy.org/doc/html/label.ht ... -statement

Fire247
Newbie
Posts: 4
Joined: Fri Jun 15, 2018 7:02 pm
Contact:

Re: if statement always false

#4 Post by Fire247 »

mitoky wrote: Fri Jun 15, 2018 8:23 pm I could be wrong on why this is the case but i think its because the text input is a string while the number is an integer. (i hope someone knows how to explain this better as i am not good with this myself)
kivik wrote: Fri Jun 15, 2018 9:09 pm So mitoky's nailed it I think that the issue is because renpy.input returns a string, so you'd be comparing "10" == 10 for example and they don't equal.
Thank you so much for your answers! The game is working just as intended now. Also, thanks for explaining what each part of the reworked code does kivik. :D

User avatar
mitoky
Veteran
Posts: 316
Joined: Sat Feb 07, 2015 9:12 pm
Projects: The Purring Demon's Love, circus eterie
Contact:

Re: if statement always false

#5 Post by mitoky »

kivik wrote: Fri Jun 15, 2018 9:09 pm So mitoky's nailed it I think that the issue is because renpy.input returns a string, so you'd be comparing "10" == 10 for example and they don't equal.

Casting the value into an int would work: int("10") == 10

However, if you cast the renpy.input directly as an int, then this statement won't ever work:

Code: Select all

        if inputzahl == "Beenden":
Since inputzahl won't ever be a string. The other problem is that the player can input other nonsensical string that cannot be converted into an int, e.g. "orange". int("orange") would throw an error and crash your game for example.

So I'd suggest the following:

Code: Select all

    python:
        a = renpy.random.randint(0, 10)
        b = renpy.random.randint(0, 10)
        del a
        del b
        a = renpy.random.randint(0, 10)
        b = renpy.random.randint(0, 10)
        summe = (a+b)
        inputzahl = renpy.input("Was ist [a] + [b]?").strip()
    if inputzahl.lower() == "beenden":
        e "Verstanden"
        jump anfang
    if str(summe) == inputzahl:
        e "Richtig! +1 Punkt!"
        $ score += 1
        jump addierenskip
    else:
        e "Leider falsch."
        jump addierenskip
First of all, instead of having so many python lines with $, just put them inside a python block :D

I've tweaked the input function to .strip() your input from any spaces before or after. This is generally good habit, because I don't know why - but people have a habit of putting space after everything! This will remove the space for you.

Next I'm testing the lower case version of inputzahl against the lowercase version of "beenden" - this means "Beenden", "BEENDEN" and any other variations of letter casing will match just fine.

Then, instead of converting your input into an int, why not convert the summe variable into a string? the str() function does that - and it should work.

I also noticed you've got some weird indentation with your labels at the start - if you mean to use local labels, please check the correct usage here: https://www.renpy.org/doc/html/label.ht ... -statement
On a side note if i may ask (learning coding still) what exactly does this part do:

Code: Select all

    python:
        a = renpy.random.randint(0, 10)
        b = renpy.random.randint(0, 10)
        del a
        del b
        a = renpy.random.randint(0, 10)
        b = renpy.random.randint(0, 10)
I understand the first two lines chose a random number between 0 and 10, but then they get deleted and then that process repeated? Why?

Fire247
Newbie
Posts: 4
Joined: Fri Jun 15, 2018 7:02 pm
Contact:

Re: if statement always false

#6 Post by Fire247 »

mitoky wrote: Sat Jun 16, 2018 12:42 pm On a side note if i may ask (learning coding still) what exactly does this part do:

Code: Select all

    python:
        a = renpy.random.randint(0, 10)
        b = renpy.random.randint(0, 10)
        del a
        del b
        a = renpy.random.randint(0, 10)
        b = renpy.random.randint(0, 10)
I understand the first two lines chose a random number between 0 and 10, but then they get deleted and then that process repeated? Why?
That's just my messy coding. I need both variables to be rerolled every time, but the first time the player reaches that part both of them aren't set yet so they can't be deleted either. I might clean that up later on.

kivik
Miko-Class Veteran
Posts: 786
Joined: Fri Jun 24, 2016 5:58 pm
Contact:

Re: if statement always false

#7 Post by kivik »

Fire247 wrote: Sat Jun 16, 2018 1:43 pm
mitoky wrote: Sat Jun 16, 2018 12:42 pm On a side note if i may ask (learning coding still) what exactly does this part do:

Code: Select all

    python:
        a = renpy.random.randint(0, 10)
        b = renpy.random.randint(0, 10)
        del a
        del b
        a = renpy.random.randint(0, 10)
        b = renpy.random.randint(0, 10)
I understand the first two lines chose a random number between 0 and 10, but then they get deleted and then that process repeated? Why?
That's just my messy coding. I need both variables to be rerolled every time, but the first time the player reaches that part both of them aren't set yet so they can't be deleted either. I might clean that up later on.
You don't need to delete variables before reassigning them - the first two lines will effectively randomise the a and b each time it's executed :)

Good spot mitoky! I hadn't even noticed it!

Post Reply

Who is online

Users browsing this forum: Semrush [Bot]