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.
-
AsHLeX
- Miko-Class Veteran
- Posts: 534
- 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:
#1
Post
by AsHLeX » Mon May 16, 2022 7:32 am
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.
-
Ocelot
- Eileen-Class Veteran
- Posts: 1882
- Joined: Tue Aug 23, 2016 10:35 am
- Github: MiiNiPaa
- Discord: MiiNiPaa#4384
-
Contact:
#2
Post
by Ocelot » Mon May 16, 2022 7:52 am
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 > >
-
AsHLeX
- Miko-Class Veteran
- Posts: 534
- 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:
#3
Post
by AsHLeX » Mon May 16, 2022 8:16 am
Thank you!! That was super helpful

Users browsing this forum: Bing [Bot]