Forums
New posts
Search forums
Members
Current visitors
Log in
Register
What's new
Search
Search
Search titles only
By:
New posts
Search forums
Menu
Log in
Register
Install the app
Install
Forums
Archive
Archive
Python
Looking for a slick way to classify relationship between twonumbers, without tons of if/else
JavaScript is disabled. For a better experience, please enable JavaScript in your browser before proceeding.
You are using an out of date browser. It may not display this or other websites correctly.
You should upgrade or use an
alternative browser
.
Reply to thread
Message
[QUOTE="palewire, post: 3895272"] 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. [/QUOTE]
Verification
Post reply
Forums
Archive
Archive
Python
Looking for a slick way to classify relationship between twonumbers, without tons of if/else
Top