(Solved)Elif Issue

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
User avatar
Yukari
Regular
Posts: 123
Joined: Sun Feb 06, 2011 6:28 am
Completed: Aozora Meikyuu, Yozora Rhapsody, Imolicious, Yume Puzzle, Apprehend;Girlfriend
Projects: Games&Girls, Fuyuzora Rumble
Organization: Yume Creations
Tumblr: yumecreationsvn
Location: Germany
Contact:

(Solved)Elif Issue

#1 Post by Yukari »

I use this code to check for the day of the game:

Code: Select all

if day == 30 or 60 or 90 or 120 or 150 or 180 or 210 or 240 or 270 or 300 or 330 or 360:
        pov "I have to pay my rent today."
        if money >= 500:
            $ money -=500
            pov "I could pay my rent this time."
        else:
            pov "I don't have enough money!\nHow can this be...?!"
            #Game Over
            return
    elif day == 365:
        pov "The End"
        return
    else:
        pass
But on every day I get the message to pay the rent. I don't know what's wrong with this.
Last edited by Yukari on Wed Jun 01, 2016 5:59 am, edited 1 time in total.
Image

User avatar
Darim
Regular
Posts: 67
Joined: Sun Jun 21, 2015 4:17 pm
Organization: Setsuna Ken
Github: SetsunaKen
Location: Germany
Contact:

Re: Elif Issue

#2 Post by Darim »

It could be an indentation error because of your tabs and spaces. Also check the variables values of money and day before entering the block.

Code: Select all

if day == 30 or 60 or 90 or 120 or 150 or 180 or 210 or 240 or 270 or 300 or 330 or 360:
    pov "I have to pay my rent today."
    if money >= 500:
        $ money -=500
        pov "I could pay my rent this time."
    else:
        pov "I don't have enough money!\nHow can this be...?!"
        #Game Over
        return
elif day == 365:
    pov "The End"
    return
else:
    pass
Viele Grüße :)

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: Elif Issue

#3 Post by trooper6 »

So, the indentation is a problem...that isn't the biggest problem, you are misunderstanding how to put together an if statement.

This:

Code: Select all

if day == 30 or 60:
means:

If day equals 30, or if 60 is True. And 60 is a number that isn't 0, so it is always true...so this whole line will always be true and you will always have rent due.

To get what you want, you'd have to write it all out by hand like so:

Code: Select all

if day == 30 or day == 60:
So, for your code, you'd want:

Code: Select all

    if day == 30 or day == 60 or day == 90 or day == 120 or day == 150 or day == 180 or day == 210 or day == 240 or day == 270 or day == 300 or day == 330 or day == 360:
        pov "I have to pay my rent today."
        if money >= 500:
            $ money -=500
            pov "I could pay my rent this time."
        else:
            pov "I don't have enough money!\nHow can this be...?!"
            #Game Over
            return
    elif day == 365:
        pov "The End"
        return
    else:
        pass
Or you could put in parenthesis to make clear what you mean:

Code: Select all

if day == (30 or 60):
Now both of those options get pretty long and would be irritating if you had to type that out multiple times. So you can do some other options. For example, you could make a list with all the values when the rent is due and then check to see if the day is in the list. That would look like this:

Code: Select all

default rentdays = [30, 60, 90, 120, 150, 180, 210, 240, 270, 300, 330, 360]

# The game starts here.
label start:
    if day in rentdays:
        pov "I have to pay my rent today."
        if money >= 500:
            $ money -=500
            pov "I could pay my rent this time."
        else:
            pov "I don't have enough money!\nHow can this be...?!"
            #Game Over
            return
    elif day == 365:
        pov "The End"
        return
    else:
        pass
Another option? I noticed that all of your rent days are divisible by 30. So instead of having a list, you could just check to see if the remainder after dividing days by 30 is 0. If it is, you know you have one of your rent days. Like so:

Code: Select all

label start:
    if day > 0 and day%30 == 0:
        pov "I have to pay my rent today."
        if money >= 500:
            $ money -=500
            pov "I could pay my rent this time."
        else:
            pov "I don't have enough money!\nHow can this be...?!"
            #Game Over
            return
    elif day == 365:
        pov "The End"
        return
    else:
        pass
So...just be thoughtful
Last edited by trooper6 on Wed Jun 01, 2016 12:37 am, edited 1 time in total.
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

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

Re: Elif Issue

#4 Post by philat »

In general, what trooper said, but % (modulo) rather than division. :)

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: Elif Issue

#5 Post by trooper6 »

philat wrote:In general, what trooper said, but % (modulo) rather than division. :)
Thanks for the catch philat! I will edit it to be correct!
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

drKlauz
Veteran
Posts: 239
Joined: Mon Oct 12, 2015 3:04 pm
Contact:

Re: Elif Issue

#6 Post by drKlauz »

Code: Select all

if x==2:
  x+=1
else:
  pass
is same as

Code: Select all

if x==2:
  x+=1
This part is useless, even harmful as it clutter your code

Code: Select all

else:
  pass
Had to note, as i see this thing too much in people's code.
I may be available for hire, check my thread: viewtopic.php?f=66&t=51350

User avatar
Yukari
Regular
Posts: 123
Joined: Sun Feb 06, 2011 6:28 am
Completed: Aozora Meikyuu, Yozora Rhapsody, Imolicious, Yume Puzzle, Apprehend;Girlfriend
Projects: Games&Girls, Fuyuzora Rumble
Organization: Yume Creations
Tumblr: yumecreationsvn
Location: Germany
Contact:

Re: Elif Issue

#7 Post by Yukari »

Thank you Trooper ^^
It works perfectly!
Image

Onishion
Veteran
Posts: 295
Joined: Mon Apr 20, 2015 10:36 am
Contact:

Re: (Solved)Elif Issue

#8 Post by Onishion »

There's another slightly easier option for some of this. Trooper including the option to have:

Code: Select all

default rentdays = [30, 60, 90, 120, 150, 180, 210, 240, 270, 300, 330, 360]

label start:
    if day in rentdays:
but you can actually shortcut that if you like (if this is something that you'd only need to access the "retdays" in this one example, at least), which is to just go with:

Code: Select all

label start:
    if day in (30, 60, 90, 120, 150, 180, 210, 240, 270, 300, 330, 360):
That works just as well, and saves some space, of course if you do this check in multiple locations then setting up a single list variable might be simpler, but options are a good thing. ;) I tend to use this model any time I want to check three or more string possibilities against a single variable.

Post Reply

Who is online

Users browsing this forum: Google [Bot], Ocelot