Solution Exercise 7

Using only simple variables and if statements, you should be able to get this to work; a loop is not needed.

Given 3 numbers ( X , Y , Z ), assign variables x, y, z so that x ≤ y ≤ z and x, y, and z are from X , Y , and Z . Use only a series of if-statements and assignment statements.

Hint:
You must define the conditions under which you choose between x← X , x← Y or x← Z . You will do a similar analysis for assigning values to y and z. Note that your analysis for setting y will depend on the value set for x; similarly, your analysis for setting z will depend on values set for x and y.

def main():
    X = 33
    Y = 101
    Z = 3
    
    if X < Y and X < Z:
        x = X
        if Y < Z:
            y = Y
            z = Z
        else:
            y = Z
            z = Y
    else:
        if Y < X and Y < Z:
            x = Y
            if X < Z:
                y = X 
                z = Z 
            else:
                y = Z 
                z = X 
        else:
            x = Z
            if X < Y:
                y = X  
                z = Y
            else:
                y = Y 
                z = X
        
    print(x,y,z)
           
if __name__ == '__main__':
    main()