Reputation: 43
I have an if statement that is picking which diameter is closest to the avg. In the statement I have the same variable equal to the vertical_tube_d for both if and else because later in the code i am writing to a file and needed one common variable. So the code is supposed to pick the closest and later in the code write that diameter.
def pick_diameter(d1, d2, avgd):
if abs(avgd-d1) < abs(avgd-d2):
vertical_tube_d = d1
else:
vertical_tube_d = d2
Is this a problem/will it confuse the computer?
Upvotes: 1
Views: 351
Reputation: 12347
There is no problem with your code. I suggest next time to test it yourself, rather than ask a Stack Overflow question, though. As a matter of style, I would prefer to write it briefly like so:
def pick_diameter(d1, d2, avgd):
vertical_tube_d = d1 if abs(avgd-d1) < abs(avgd-d2) else d2
SEE ALSO:
Python ? (conditional/ternary) operator for assignments
Upvotes: 2