P
palewire
In my application, I'd like to have a function that compares two values,
either of which may be null, and then classify the result depending on
whether one is higher or lower than the other.
I find myself writing something clunky like this, and I'm curious whether
anyone might know a more elegant way for parsing this out.
def compare(a, b):
if not a and not b:
return "No data"
else:
if a == 0 and b == 0:
return "Never had"
else:
if a == b:
return "Stayed the same"
elif a < b:
return "Gained"
elif a > b and b > 0:
return "Lost Some"
elif a > b and b == 0:
return "Lost All"
If there's some obvious way to search for this solution online, feel free to
slap me with that. I tried digging around on Google and couldn't come up
with much.
Thanks much.
either of which may be null, and then classify the result depending on
whether one is higher or lower than the other.
I find myself writing something clunky like this, and I'm curious whether
anyone might know a more elegant way for parsing this out.
def compare(a, b):
if not a and not b:
return "No data"
else:
if a == 0 and b == 0:
return "Never had"
else:
if a == b:
return "Stayed the same"
elif a < b:
return "Gained"
elif a > b and b > 0:
return "Lost Some"
elif a > b and b == 0:
return "Lost All"
If there's some obvious way to search for this solution online, feel free to
slap me with that. I tried digging around on Google and couldn't come up
with much.
Thanks much.