On Mon, Jul 1, 2013 at 10:59 PM, Neil Cerutti <[email protected]> wrote:
> On 2013-06-30, Chris Angelico <[email protected]> wrote:
>> So, here's a challenge: Come up with something really simple,
>> and write an insanely complicated - yet perfectly valid - way
>> to achieve the same thing. Bonus points for horribly abusing
>> Python's clean syntax in the process.
>>
>> Go on, do your worst!
>
> I've often thought it was redundant for Python to support 'if'
> when it has dictionaries, cf the rationale for having no
> 'switch'.
>
> valid_name = None
> while not valid_name:
> name = input("Enter your name: ")
> valid_name = {
> True: lambda: print("No name longer than 20 letters."),
> False: lambda: True,
> }[len(name) > 20]()
>
> Much better.
Good! Good! But, waaaah. Waaaah.
def get_name():
while True:
name = input("Enter your name: ")
yield {
True: lambda: print("No name longer than 20 letters."),
False: lambda: name,
}[len(name) > 20]()
name = next(filter(None,get_name()))
ChrisA
--
http://mail.python.org/mailman/listinfo/python-list