(SOLVED) Calculating % in python while ensuring that it never divides by 0

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
AsHLeX
Miko-Class Veteran
Posts: 556
Joined: Wed Dec 25, 2013 1:09 pm
Completed: Starlight Dreamers, Mysterious Melody, Town of Memories, Marked, To Fly, The Change, Him From The Past, A Forgotten Memory
Projects: Cafe Mysteria
Location: Malaysia
Contact:

(SOLVED) Calculating % in python while ensuring that it never divides by 0

#1 Post by AsHLeX »

Hi there! So as the subject title says, I'm trying to calculate a percentage of a stat in python but at the same time making sure that it never ends up dividing by zero. What I was trying to achieve is:

Code: Select all

label start:
          $ joker = 0 # Personality stat
          $ joker_max = 0 # Maximum value of personality stat
... ...
# Every time the player comes across an menu that has an option with a joker personality stat in it, joker_max increases by 1
label menu_option_that_has_a_joker_in_it:
          $ joker_max += 1
          menu:
                  "Tell a joke":
                           $ joker += 1
                  "Don't tell a joke":
                           pass
          jump route_rejoin
# If the player doesn't come across an option with a joker personality in it, joker_max remains the same.
label alternate_route_that_doesnt_have_joker_opt_in_it:
        jump route_rejoin
... ...
# The problem arises here. If the player hasn't come across a route with a joker option in it, it'll end up dividing by 0 and lead to an undefined error...
label route_rejoin:
         if joker/joker_max * 100 > 50:
                  jump joker_dialogue
        else:
                  jump normal_dialogue
Maybe I'm just bad at math equations or asking for the impossible, but is there a way that I can rephrase the last part so that it won't ever lead to a division by 0 (and thus an undefined error)?

Thanks in advance!
Last edited by AsHLeX on Mon May 16, 2022 8:16 am, edited 1 time in total.

User avatar
Ocelot
Lemma-Class Veteran
Posts: 2405
Joined: Tue Aug 23, 2016 10:35 am
Github: MiiNiPaa
Discord: MiiNiPaa#4384
Contact:

Re: Calculating % in python while ensuring that it never divides by 0

#2 Post by Ocelot »

Check if joker_max is 0 before proceeding. You can wrap all that in nice function:

Code: Select all

init python:
    def calculate_percentage(cur, max, default_value=0):
        if max == 0:
            return default_value
        return int(cur * 100 / max)

# . . .

label route_rejoin:
         if calculate_percentage(joker, joker_max) > 50:
                  jump joker_dialogue
        else:
                  jump normal_dialogue
< < insert Rick Cook quote here > >

User avatar
AsHLeX
Miko-Class Veteran
Posts: 556
Joined: Wed Dec 25, 2013 1:09 pm
Completed: Starlight Dreamers, Mysterious Melody, Town of Memories, Marked, To Fly, The Change, Him From The Past, A Forgotten Memory
Projects: Cafe Mysteria
Location: Malaysia
Contact:

Re: Calculating % in python while ensuring that it never divides by 0

#3 Post by AsHLeX »

Thank you!! That was super helpful :)
Image
New demo out 24/12/23!!

Post Reply

Who is online

Users browsing this forum: No registered users