[Solved] Format string problem

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
Uy27
Regular
Posts: 29
Joined: Wed Apr 04, 2018 2:07 am
Contact:

[Solved] Format string problem

#1 Post by Uy27 »

So I'm messing around with a code made my trooper6 from here viewtopic.php?t=39482, which works no problem.

My problem is here:

Code: Select all

init python:        
    class PointLocker(object):
        def __init__(self):
            self.dp = 0
            self.np = 0
            
        def ch_pts(self, type, num):
            if num >= 0:
                start = "{color=#6abd45}{size=30}+"
            else:
                start = "{color=#ed2024}{size=30}"
            end = "{/size}{/color}{color=#ffee2e}{size=12}%s{/size}{/color}{image=images/menu/heart_icon.png}" % ("DIDAC" if type == 'dp' else "NEUS")
            message = "%s %d %s" % (start, num, end)
            if type == "dp":
                self.dp += num
            else:
                self.np += num
            renpy.show_screen("Outer_{}Screen".format(type), message)
I'm talking about this line here:

Code: Select all

  % ("DIDAC" if type == 'dp' else "NEUS")
Here is where I'm having trouble because what to do if you want to add MORE names beside didac and neus.
In this code, it's restricted to 2 variables.
I want to have several but when I remove the "Else" and add stuff like "If" or "elif" it simply gives me a syntax error.

Is there a way to add more than those two after the % sign? This here to be specific ("DIDAC" if type == 'dp' else "NEUS"). I spent several hours now and I'm out of ideas. Only thing I can think of is rewriting the code but I won't make anything as good as troopers so I want to ask for a bit of help before giving up.

*edit* To clarify it a bit more;
I want all of these "aaaa" if type == 'aa', "bbbb" if type == 'bb', "cccc" if type == 'cc' etc in the same line as where the original is.
Last edited by Uy27 on Wed May 30, 2018 10:12 pm, edited 1 time in total.

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

Re: format string problem

#2 Post by philat »

Just pull the variable out into a regular if/else. You do not want to nest ternary operators, it's hell to read.

Uy27
Regular
Posts: 29
Joined: Wed Apr 04, 2018 2:07 am
Contact:

Re: format string problem

#3 Post by Uy27 »

I'm a bit of a newbie. How exactly does someone go about that with this kind of code?
Also what is ternary operators?

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

Re: format string problem

#4 Post by philat »

Ternary operator refers to the "a if b else c" structure in the line you mentioned. It's best suited for one condition (i.e., two possible results).

As for if/else, the code you linked has an example.

Code: Select all

            if type == "dp":
                self.dp += num
            else:
                self.np += num

Uy27
Regular
Posts: 29
Joined: Wed Apr 04, 2018 2:07 am
Contact:

Re: format string problem

#5 Post by Uy27 »

I see so as I though it's not possible to add more to that code I have to rewrite it.
Thank you for the help, I will try to play around with it again and see if I can make it work this time.

*Edit* So for people who may have the same problem, I solved it by simply making 3 copy paste of the whole code. Messy? Yes, but it works. Spent way too many hours trying to solve this.

Post Reply

Who is online

Users browsing this forum: Bing [Bot]