Variable containing a word

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
Skiegh
Regular
Posts: 38
Joined: Mon Dec 19, 2011 2:47 pm
Contact:

Variable containing a word

#1 Post by Skiegh »

Hello, hello. I was just wondering if it's possible when checking a variable with an if statement or something, to check for a specific phrase within that variable instead. Say if I had multiple options during a choice, and later had dialogue associated with maybe two of those, rather than stipulate each tiny difference within the variable, if I could just see if it contained part of it.

To make it simple, here's the choice and the options it could be...

$ Choice1 = "Pos"
$ Choice1 = "Pos Mary"
$ Choice1 = "Pos John"
$ Choice1 = "Swa"
$ Choice1 = "Swa Mary"
$ Choice1 = "Swa John"

Is it possible to just look to see if Choice1 contains either "Swa" or "Pos" rather than specify the whole thing? If not, since I'm always learning, any alternatives with similar results?

Thanks in advance.

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

Re: Variable containing a word

#2 Post by philat »

Code: Select all

if "Pos" in Choice1:
    "True"
else:
    "False"
Do note that this would also return true for "Pos" existing anywhere in the string (for instance, hypothetically, "Swa Position" or something like that).

User avatar
Remix
Eileen-Class Veteran
Posts: 1628
Joined: Tue May 30, 2017 6:10 am
Completed: None... yet (as I'm still looking for an artist)
Projects: An un-named anime based trainer game
Contact:

Re: Variable containing a word

#3 Post by Remix »

For more *precision*...

Code: Select all

if Choice1.startswith( "Pos" ):
    "True: Startswith Pos"
    
elif Choice1.endswith( "Mary" ):
    "True: Endswith Mary"

elif Choice1[:3] == "Pos": # first 3 letters
    "True: Starts Pos"

elif Choice1[-4:].lower() == "john": # final 4 letters lowercased
    "True: Ends john"

else:
    "False"
Frameworks & Scriptlets:

Skiegh
Regular
Posts: 38
Joined: Mon Dec 19, 2011 2:47 pm
Contact:

Re: Variable containing a word

#4 Post by Skiegh »

Awesome, thank you both the information! I will try those out next time I get a chance.

Thanks!

Skiegh
Regular
Posts: 38
Joined: Mon Dec 19, 2011 2:47 pm
Contact:

Re: Variable containing a word

#5 Post by Skiegh »

Rather than make a new thread, I figure I should just ask it here, which is basic advice, I suppose. The above question is the result of me including a short little card game. While I would love to code an entire card game (do not have the skills), it's a mildly scripted thing, but there are variables based on what you play and when. So rather than have a ton of variables for every player, I was thinking just having one, which contained multiple strings/shortenings that sort of represented the cards the player had and so on.

While the original question is something that will help me in other places, I am now wondering if it's possible to not just have an if statement that is looking for a certain word, but can I add or remove specific parts of a variable by doing this?

So, let's use another example...

$ Testing = "c.12 c.4 c.1 h:13"

Can I search for c.1 and replace it or remove it, while leaving the rest of the content of the variable in-tact? Thanks in advance for answers on this.

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: Variable containing a word

#6 Post by trooper6 »

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

travoljohn
Newbie
Posts: 1
Joined: Tue Oct 16, 2018 2:21 am
Contact:

Re: Variable containing a word

#7 Post by travoljohn »

More on.....Python List

Post Reply

Who is online

Users browsing this forum: Bing [Bot], Kocker