Teaching Programming

superpollo utente at esempio.net
Tue May 4 14:35:48 EDT 2010


superpollo ha scritto:
> superpollo ha scritto:
>> James Mills ha scritto:
>>> On Tue, May 4, 2010 at 9:56 PM, superpollo <utente at esempio.net> wrote:
>>>> of course! *but* if i must generate on-the-fly python code that 
>>>> defines a
>>>> function i am back again to the problem:
>>>
>>> One-liner:
>>>
>>> $ python
>>> Python 2.6.5 (r265:79063, Apr 27 2010, 18:26:49)
>>> [GCC 4.4.1 (CRUX)] on linux2
>>> Type "help", "copyright", "credits" or "license" for more information.
>>>>>> a, b = 2, 3
>>>>>> print a + b if a > b else a**b - b**2
>>> -1
>>>>>> a, b = 3, 2
>>>>>> print a + b if a > b else a**b - b**2
>>> 5
>>>
>>> --James
>>
>> what if i want an elif clause?
>>
> 
> my first try obviously falied:
> 
>  >>> print a + b if a > b elif a=b "WOW" else a**b - b**2
>   File "<stdin>", line 1
>     print a + b if a > b elif a=b "WOW" else a**b - b**2
>                             ^
> SyntaxError: invalid syntax
> 

ok i got it:

 >>> a,b=2,3
 >>> print a + b if a > b else "WOW" if a < b else a**b - b**2
WOW
 >>>



More information about the Python-list mailing list