A technique from a chatbot

Michael F. Stemper michael.stemper at gmail.com
Wed Apr 3 17:36:30 EDT 2024


On 03/04/2024 13.45, Gilmeh Serda wrote:
> On 2 Apr 2024 17:18:16 GMT, Stefan Ram wrote:
> 
>> first_word_beginning_with_e
> 
> Here's another one:
> 
>>>> def ret_first_eword():
> ...     return [w for w in ['delta', 'epsilon', 'zeta', 'eta', 'theta'] if w.startswith('e')][0]
> ...
>>>> ret_first_eword()
> 'epsilon'

Doesn't work in the case where there isn't a word starting with 'e':

  >>> def find_e( l ):
  ...   return [w for w in l if w.startswith('e')][0]
  ...
  >>> l = ['delta', 'epsilon', 'zeta', 'eta', 'theta']
  >>> find_e(l)
  'epsilon'
  >>> l = ['The','fan-jet','airline']
  >>> find_e(l)
  Traceback (most recent call last):
    File "<stdin>", line 1, in <module>
    File "<stdin>", line 2, in find_e
  IndexError: list index out of range
  >>>


-- 
Michael F. Stemper
If it isn't running programs and it isn't fusing atoms, it's just bending space.



More information about the Python-list mailing list