Is it possible in Python to write a sort of truth table to simplify the writing of if statements? -
let's i'm trying print out orientation of tablet device using accelerometer provides acceleration measurements in horizontal , vertical directions of display of device. know such printout done using set of if
statements in form such following:
if abs(stableacceleration[0]) > abs(stableacceleration[1]) , stableacceleration[0] > 0: print("right") elif abs(stableacceleration[0]) > abs(stableacceleration[1]) , stableacceleration[0] < 0: print("left") elif abs(stableacceleration[0]) < abs(stableacceleration[1]) , stableacceleration[1] > 0: print("inverted") elif abs(stableacceleration[0]) < abs(stableacceleration[1]) , stableacceleration[1] < 0: print("normal")
would possible codify logic of in neater form? sort of truth table constructed such orientation lookup value of table? way this?
edit: following a suggestion @jonrsharpe, have implemented logic in way following:
tableorientations = { (true, true): "right", (true, false): "left", (false, true): "inverted", (false, false): "normal" } print( tableorientations[( abs(stableacceleration[0]) > abs(stableacceleration[1]), stableacceleration[0] > 0 )] )
consider doing along lines of this:
x = 0; if abs(stableacceleration[0]) > abs(stableacceleration[1]) : x += 2 if stableacceleration[0] > 0: x +=1 list = ["normal", "invert", "left", "right"] print(list[x])
that being said, series of if
statements don't cover every case.
Comments
Post a Comment