Answer:
def GetInRange(x, a, b):
if (a > b): a, b = b, a
if (x < a): return a;
if (x > b): return b;
return x;
Explanation:
By swapping a and b if a>b you can proceed under the assumption that you only have to check if x<a or x>b, which simplifies the logic a lot.