mercredi 29 avril 2015

Is it possible in Python to write a sort of truth table to simplify the writing of if statements?

Let's say I'm trying to print out the orientation of a tablet device using its accelerometer that provides acceleration measurements in the horizontal and vertical directions of the display of the device. I know that such a printout could be done using a set of if statements in a form such as the following:

if abs(stableAcceleration[0]) > abs(stableAcceleration[1]) and stableAcceleration[0] > 0:
    print("right")
elif abs(stableAcceleration[0]) > abs(stableAcceleration[1]) and stableAcceleration[0] < 0:
    print("left")
elif abs(stableAcceleration[0]) < abs(stableAcceleration[1]) and stableAcceleration[1] > 0:
    print("inverted")
elif abs(stableAcceleration[0]) < abs(stableAcceleration[1]) and stableAcceleration[1] < 0:
    print("normal")

Would it be possible to codify the logic of this in some neater form? Could a sort of truth table be constructed such that the orientation is simply a lookup value of this table? What would be a good way to do something like this?

Aucun commentaire:

Enregistrer un commentaire