Skip to content Skip to sidebar Skip to footer

Evaluating Input Function Inline, In Order To Append Input Or Placeholder To A List

I'm having some fun with Python inline functions, and wonder if this is even possible. I'm using the click module, and I'm using its prompt function and I will add the entered valu

Solution 1:

This will work:

item.append(click.prompt("Name", default='') or '** empty **')

Your expression was

click.prompt("Name", default = '') if not '' else '** empty **'

This is the same as:

if not '':
    item.append(click.prompt("Name", default=''))
else:
    item.append('** empty **')

But not '' is always true, because '' is always false. So you were always appending the string the user entered, including the empty string.


Solution 2:

@Ned's solution is the best:

item.append(click.prompt("Name", default='') or '** empty **')

Or to do something that's kind of similar to what you did:

c=click.prompt("Name", default = '')
item.append(c if c else '** empty **')

Or of course can do, (but runs click.prompt("Name", default = '') twice, so better just once):

item.append(click.prompt("Name", default = '') if click.prompt("Name", default = '') else '** empty **')

Post a Comment for "Evaluating Input Function Inline, In Order To Append Input Or Placeholder To A List"