[ SOLVED ] Comparing Strings in Python?

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
xylia
Newbie
Posts: 6
Joined: Wed Oct 20, 2010 1:35 am
Contact:

[ SOLVED ] Comparing Strings in Python?

#1 Post by xylia »

I'm trying to compare the string that I'm getting from user input with a pre-determined string in order to add to the stat (intelligence) determined by the dating sim framework.

So I have

Code: Select all

$ yourname = renpy.input("What is your name?", "First name only!", length=40) #Asks for name input
Which prints "%(yourname)s" as the input (IE "JOHN")

And then to test...

Code: Select all

if  "%(yourname)s" == "JOHN":
        $ inteligence += 30
If I run $ intelligence += 30 outside of the if statement, it works, so I'm sure that's not the problem.

I think it's just not comparing "%(yourname)s" and "JOHN" the way that I want it to, which is if someone types in "JOHN," it will compare "JOHN" with "JOHN".
Last edited by xylia on Thu Oct 21, 2010 1:26 am, edited 1 time in total.

fortaat
Regular
Posts: 183
Joined: Tue May 18, 2010 1:16 pm
Contact:

Re: Comparing Strings in Python?

#2 Post by fortaat »

Try:

Code: Select all

$ yourname = renpy.input("What is your name?", "First name only!", length=40) #Asks for name input

python:

	if  yourname == "JOHN":
       		 $ inteligence += 30

xylia
Newbie
Posts: 6
Joined: Wed Oct 20, 2010 1:35 am
Contact:

Re: Comparing Strings in Python?

#3 Post by xylia »

It worked! LOL don't I feel silly...

THANK YOU

by the way, my friend also pointed out that I spelled intelligence wrong...

Guess how I feel... ):

fortaat
Regular
Posts: 183
Joined: Tue May 18, 2010 1:16 pm
Contact:

Re: Comparing Strings in Python?

#4 Post by fortaat »

Stoopid?

Anyway, try this if you're troubled by spelling.

Incendium
Regular
Posts: 25
Joined: Wed Dec 09, 2009 12:57 am
Contact:

Re: Comparing Strings in Python?

#5 Post by Incendium »

Keep in mind that string comparisons are case-sensitive in Python, so if you want to match "JOHN" with something like "John" or "john", you will need to do something like this:

Code: Select all

if yourname.upper() == "JOHN":
    $ inteligence += 30

Post Reply

Who is online

Users browsing this forum: No registered users