Jan-20-2019, 12:09 PM
Hi, so I am supposed to write a function to find the maximum of 3 numbers. Initially I wanted to write a function that takes in 3 values and analyzes each case by case , like this :
So I looked at the solution and this is what it gave :
def Max ( x, y, z) if x > y and y > z print x elif x > y and y < z print z .... so on and so forthBut I realized that it is very tedious because there are quite a few possible cases.
So I looked at the solution and this is what it gave :
def max_of_two( x, y ): if x > y: return x return y def max_of_three( x, y, z ): return max_of_two( x, max_of_two( y, z ) ) print(max_of_three(3, 6, -5))But I don't understand how this code works. Isn't the code for max_of_two missing an else statement? Because it should return x if x > y and return y if x < y. I'm confused...