Function to take the minimum of 3 numbers
Ben Bacarisse
ben.usenet at bsb.me.uk
Sun Oct 9 09:09:04 EDT 2016
Cai Gengyang <gengyangcai at gmail.com> writes:
> I'm moving on to chapter 9
> (http://programarcadegames.com/index.php?lang=en&chapter=lab_functions)
> of programarcadegames for the time being and going back to chapter 8
> later (its just fucking frustrating and doesn't seem to work for the
> time being and I have a very bad temper).
>
> At least for chapter 9, I got the first part correct at first try ---
> define a function that takes and prints the smallest of 3 numbers. I
> pasted it here for reference and discussion.
Presumably you must do this without Python's min function?
> The code can also be
> further modified to include a clause that says that if two numbers tie
> for smallest, choose any of the two numbers.
What would be the point? Your code already does that.
>>>> def min3(a, b, c):
> min3 = a
> if b < min3:
> min3 = b
> if c < min3:
> min3 = c
> if b < c:
> min3 = b
> return min3
The last if is not needed.
Forced to pretend that there is no min function already, I'd be tempted
to write
def min3(a, b, c):
def min2(x, y):
return x if x < y else y;
return min2(a, min2(b, c))
--
Ben.
More information about the Python-list
mailing list