[Tutor] Beginner's question

Peter Otten __peter__ at web.de
Fri Nov 23 10:25:52 CET 2012


Steven D'Aprano wrote:

> On 23/11/12 01:56, Peter O'Doherty wrote:
> 
>> This code appears to work although it's very cumbersome. Is
>>there a better way to do it?
> 
> 
> Of course it is cumbersome, that's because of the artificial
> constraints set on the problem. I quote from your description

Indeed.

> Finally, here is how I would solve the problem for real:
> 
> 
> try:
>      print max(filter(lambda n: n%2 != 0, (x, y, z)))
> except ValueError:
>      print ('no largest odd number')
> 
> 
> You are not expected to understand this! Especially not the
> mysterious lambda.

For completeness here is the version you *are* supposed to understand, right 
now or "real soon":

numbers = x, y, z
odds = [n for n in numbers if n % 2]
if odds:
    print("largest odd number:", max(odds))
else:
    print("no odd numbers")



More information about the Tutor mailing list