Simple way to test a string of values in an if statement?

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
Dreamwisp
Newbie
Posts: 2
Joined: Sun Aug 24, 2014 7:07 am
Contact:

Simple way to test a string of values in an if statement?

#1 Post by Dreamwisp »

Right now I have a long list of values I want to test against one variable, all to get the same outcome.

Sort of like this:

Code: Select all

    if name == "Bob" or name == "Joe" or name == "Dan" or name == "Jim" or name == "Rob":
        e "That's a three letter name alright."
is there a simpler way to go about this then writing out the "or name==" part over and over? Thankyou!

User avatar
xela
Lemma-Class Veteran
Posts: 2481
Joined: Sun Sep 18, 2011 10:13 am
Contact:

Re: Simple way to test a string of values in an if statement

#2 Post by xela »

Code: Select all

if name in ("Bob", "Joe", "Dan", "Jim", "Rob"):
    # Meow
Another option:

Code: Select all

if len(name) == 3:
   e "That's a three letter name alright."
But name could as well be 111 with this approach, you'll have to do try/except or isinstance check if you want more control.
Like what we're doing? Support us at:
Image

Dreamwisp
Newbie
Posts: 2
Joined: Sun Aug 24, 2014 7:07 am
Contact:

Re: Simple way to test a string of values in an if statement

#3 Post by Dreamwisp »

Thankyou very much!

I'm still very new to Ren'py, what do try, except and isinstance checks do?

I have been reading through the documentation, but this is my first real go at coding so I'm still developing the logical mindset and theres alot I don't understand yet :)

User avatar
xela
Lemma-Class Veteran
Posts: 2481
Joined: Sun Sep 18, 2011 10:13 am
Contact:

Re: Simple way to test a string of values in an if statement

#4 Post by xela »

Those are python statements, search google instead of documentation. But those are not really needed.

Tutorials point has imo by far the best collection of the most useful Python functionality.

Ren'Py documentation for Ren'Py stuff.
Like what we're doing? Support us at:
Image

Post Reply

Who is online

Users browsing this forum: No registered users